2014-04-04 88 views
4

我有以下類'schakeling',用EF生成,代表數據庫表'schakeling'。在數據庫中,'id'是主鍵,'plc_id'是外鍵。如何確定PropertyType是否是外鍵

public partial class schakeling 
{ 
    public schakeling() 
    { 
     this.verbruik = new HashSet<verbruik>(); 
    } 

    public int id { get; set; } 
    public int plc_id { get; set; } 
    public string var_output { get; set; } 
    public string omschrijving { get; set; } 
    public int vermogen { get; set; } 
    public Nullable<bool> status { get; set; } 
    public Nullable<byte> hourOn { get; set; } 
    public Nullable<byte> minuteOn { get; set; } 
    public Nullable<byte> hourOff { get; set; } 
    public Nullable<byte> minuteOff { get; set; } 

    public virtual plc plc { get; set; } 
    public virtual ICollection<verbruik> verbruik { get; set; } 
} 

我有一個視圖類

public class SchakelingsListViewModel 
{ 
    public IEnumerable<schakeling> List { get; set; } 
    public PagingInfo PagingInfo { get; set; }//Not relevant for this question 
    //And some more properties...also not relevant 
} 

我有以下視圖(省略了一些HTML爲簡潔起見)

@model PortalControl.ViewModels.SchakelingsListViewModel 
<h2>Index</h2> 
<table> 
@foreach (var item in Model.List) { 
    @Html.TableRowFor(item) 
} 
</table> 

我有一個通用的HTML輔助方法TableRowFor因爲我想能夠在EF生成的其他域實體上使用該方法。該方法生成簡單的表格數據。

public static MvcHtmlString TableRowFor<T>(this HtmlHelper helper, T obj) 
{ 
    string controller = obj.GetType().BaseType.Name; 
    string id = obj.GetType().GetProperty("id").GetValue(obj).ToString(); 

    StringBuilder sb = new StringBuilder("<tr>"); 
    sb.Append("<td>"); 
    sb.Append("<a href='" + controller + "/Edit/" + id + "'><img src='/Images/edit-icon.png' /></a>"); 
    sb.Append("<a href='" + controller + "/Details/" + id + "'><img src='/Images/details-icon.png' /></a>"); 
    sb.Append("<a href='" + controller + "/Delete/" + id + "'><img src='/Images/delete-icon.png' /></a>"); 
    sb.Append("</td>"); 

    foreach (var property in obj.GetType().GetProperties()) 
    { 
     //If statement below filters out the two virtual properties(plc, verbruik) of the schakeling class(as said, generated with EF), somewhat ugly but it works, better suggestions are welcome.. 
     if ((!property.PropertyType.Name.ToLower().Contains("icollection")) && (property.PropertyType.CustomAttributes.Count() != 0)) 
     { 
      sb.Append("<td>"); 
      //if property is foreign key 
       //sb.Append("<a href='" + correctControllerNameHere + "/Details/" + property.GetValue(obj) + "'><img src='/Images/details-icon.png' /></a>"); 
      //else 
       //sb.Append(property.GetValue(obj)); 
      sb.Append("</td>"); 
     } 
    } 
    sb.Append("</tr>"); 
    return new MvcHtmlString(sb.ToString()); 
} 

我已經是個問題,我想創建一個鏈接,如果一個屬性是一個外鍵。

我已經搜索了互聯網,但我不是PropertyInfo,MetaDataClassType和其他類似的專家。類似property.isForeign()會很可愛,但任何有效的東西都會被讚賞。

回答

1

您可以通過這種方法得到實體框架概念模型參考導航屬性:

IEnumerable<string> GetReferenceProperies<T>(DbContext context) 
{ 
    var oc = ((IObjectContextAdapter)context).ObjectContext; 
    var entityType = oc.MetadataWorkspace.GetItems(DataSpace.OSpace) 
         .OfType<EntityType>() 
         .FirstOrDefault (et => et.Name == typeof(T).Name); 
    if (entityType != null) 
    { 
     foreach (NavigationProperty np in entityType.NavigationProperties 
       .Where(p => p.ToEndMember.RelationshipMultiplicity 
            == RelationshipMultiplicity.One 
         || p.ToEndMember.RelationshipMultiplicity 
            == RelationshipMultiplicity.ZeroOrOne)) 
     { 
      yield return np.Name; 
     } 
    } 
} 

它變得有0..1在關聯結束時,所有的導航性能,使排除集合導航屬性。

現在您可以使用屬性名稱來獲取匹配PropertyInfo s並獲取屬性的值。

2

我知道它使用你的方法,非常感謝。對於其他人,請記住,如果你使用的命名慣例像我這樣的(不過,你可以使用格特的方法作爲指導,鋪平道路,爲你自己的解決方案的方式)這個解決方案僅適用:

  • 給外鍵完全相同的名稱,因爲它是指
  • db表的名稱結尾的名字與「_id」

例子:

您有一個名爲用戶數據庫表,並希望創建一個名爲bill的新數據庫表。要引用一項法案,使用FK的用戶,所以在我的情況我會命名爲FK user_id說明

更新方法TableRowFor:

public static MvcHtmlString TableRowFor<T>(this HtmlHelper helper, T obj) 
{ 
    string controller = obj.GetType().BaseType.Name; 
    string id = obj.GetType().GetProperty("id").GetValue(obj).ToString(); 

    StringBuilder sb = new StringBuilder("<tr>"); 
    sb.Append("<td>"); 
    sb.Append("<a href='" + controller + "/Edit/" + id + "'><img src='/Images/edit-icon.png' /></a>"); 
    sb.Append("<a href='" + controller + "/Details/" + id + "'><img src='/Images/details-icon.png' /></a>"); 
    sb.Append("<a href='" + controller + "/Delete/" + id + "'><img src='/Images/delete-icon.png' /></a>"); 
    sb.Append("</td>"); 

    List<string> referencePropertyList = GetReferenceProperies<T>(new NameOfDB()).ToList(); 
    foreach (var property in obj.GetType().GetProperties()) 
    { 
     //If statement below filters out the two virtual properties(plc, verbruik) of the schakeling class(as said, generated with EF), somewhat ugly but it works, better suggestions are welcome.. 
     if ((!property.PropertyType.Name.ToLower().Contains("icollection")) && (property.PropertyType.CustomAttributes.Count() != 0)) 
     { 
      sb.Append("<td>"); 
      //if property is foreign key 
      if (referencePropertyList != null && property.Name.Length >= 3 && referencePropertyList.Contains(property.Name.Substring(0, property.Name.Length - 3))) 
       sb.Append("<a href='" + property.Name.Substring(0, property.Name.Length - 3) + "/Details/" + property.GetValue(obj) + "'>" + property.GetValue(obj) + "</a>"); 
      else 
       sb.Append(property.GetValue(obj)); 
      sb.Append("</td>"); 
     } 
    } 
    sb.Append("</tr>"); 
    return new MvcHtmlString(sb.ToString()); 
}