Question

I have the following class 'schakeling', generated with EF, representing a database table 'schakeling'. In the database 'id' is the primary key and 'plc_id' is a foreign key.

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; }
}

I have a view class

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
}

I have the following view (omitted some HTML for brevity)

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

I have a generic html helper method TableRowFor because I want to be able to use the method on other domain entities generated with EF. The method generates simple table data.

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());
}

The question I have is, I would like to create a link if a property is a foreign key.

I have searched the internet but I am no expert in PropertyInfo, MetaDataClassType and other similar classes. Something like property.isForeign() would be lovely but anything that works will be appreciated.

Was it helpful?

Solution

You can get reference navigation properties from the Entity Framework conceptual model by this method:

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;
        }
    }
}

It gets all navigation properties that have 0..1 at the end of the association, so that excludes collection navigation properties.

Now you can use the property names to get the matching PropertyInfos and get the value of the properties.

OTHER TIPS

I got it working using your method, many thanks. For others, keep in mind this solution only works if you use a naming convention like mine(however, you can use the method of Gert as a guidance to pave the way for you're own solution):

  • Give a foreign key the exact same name as the the name of the db table it refers to
  • End the name with '_id'

Example:

You have a db table named user and want to create a new db table named bill. You want to refer a bill to an user using a FK, so in my situation I would name the FK user_id

Updated method 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());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top