我有以下類'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()會很可愛,但任何有效的東西都會被讚賞。