如何确定PropertyType是否为外键

时间:2014-04-04 09:54:46

标签: c# asp.net-mvc entity-framework foreign-keys html-helper

我有以下课程' 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; }
}

我有一个视图类

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()之类的东西很可爱,但任何有效的东西都会受到赞赏。

2 个答案:

答案 0 :(得分:2)

您可以通过此方法从Entity Framework概念模型中获取参考导航属性:

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并获取属性的值。

答案 1 :(得分:2)

我使用你的方法让它工作,非常感谢。对于其他人,请记住,只有使用像我这样的命名约定时,此解决方案才有效(但是,您可以使用Gert的方法作为指导为您铺平道路并拥有自己的解决方案):

  • 为外键提供与其引用的db表名称完全相同的名称
  • 使用&#39; _id&#39;
  • 结束名称

示例:

您有一个名为user的db表,并且想要创建一个名为bill的新db表。您想使用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());
}