如何确定PropertyType是否为外键

Kip*_* ei 5 c# asp.net-mvc entity-framework html-helper foreign-keys

我有以下类'schakeling',用EF生成,表示数据库表'schakeling'.在数据库中,'id'是主键,'plc_id'是外键.

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; }
}
Run Code Online (Sandbox Code Playgroud)

我有一个视图课

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
}
Run Code Online (Sandbox Code Playgroud)

我有以下视图(为简洁省略了一些HTML)

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

我有一个通用的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());
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果属性是外键,我想创建一个链接.

我搜索过互联网,但我不是PropertyInfo,MetaDataClassType和其他类似类的专家.像property.isForeign()这样的东西很可爱,但任何有效的东西都会受到赞赏.

Ger*_*old 2

您可以通过以下方法从实体框架概念模型获取参考导航属性:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

0..1它获取关联末尾的所有导航属性,因此不包括集合导航属性。

现在您可以使用属性名称来获取匹配的PropertyInfos 并获取属性的值。