How to link tables in Entity Framework

asp.net asp.net-mvc c# entity-framework

Question

I'm attempting to use MVC and Entity Framework to build an asset tracking system. There is already a database. The Location field in my ComputerInventory table serves as a foreign key to the LocationID of the Location table. I want to see the Location field of the corresponding LocationID rather than the LocationID itself when I output information about the inventory.

Even though I've been using certain tutorials, I can't seem to adjust them to fit my needs. This seems to be a rather typical occurrence, so perhaps I'm missing something. My computer inventory is shown here:

[Table("ComputerInventory")]
public class ComputerInventory
{
    [Key]
    public String AssetTag {get; set;}
    //public Int32 Location { get; set; }

    [ForeignKey("Location")]
    public virtual Location Location { get; set; }

    public Int32 PoolType { get; set; }
    public Int32 Reason { get; set; }
    public Int32 InventoryStatus { get; set; }
    public Int32 InventoryType { get; set; }
    [StringLength(500)]
    public String Comments { get; set; }
    //public String Clock { get; set; }

   // public ICollection<Location> Locations { get; set; }
}

public class AssetDBContext : DbContext
{
    public DbSet<ComputerInventory> ComputerInventory { get; set; }

    public AssetDBContext()
        : base("Name=LaptopLoaner_RW")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Entity<ComputerInventory>().HasRequired(l => l.Location).WithMany();
    }
}

Here is my model of a location.

[Table("Location")]
    public class Location
    {
        [Key]
        public Int32 Id { get; set; }
        //[Column(LocationName="LocationName")]
        [MaxLength(100)]
        public String LocationName { get; set; }
        [MaxLength(25)]
        public String Country { get; set; }
    }

    public class LocationDBContext : DbContext
    {
        public DbSet<Location> Locations { get; set; }

        public LocationDBContext()
            : base("Name=<name>")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }

I am attempting to output the data in the view as follows:

@model IEnumerable<LaptopLoaner.Models.ComputerInventory>
@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.AssetTag)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.InventoryStatus)
    </td>
    <td>
      @Html.DisplayFor(modelItem => item.Location.LocationName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.PoolType)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Reason)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.InventoryType)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Comments)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.AssetTag }) |
        @Html.ActionLink("Details", "Details", new { id=item.AssetTag }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.AssetTag })
    </td>
</tr>

}

Should I access the Location from the view or modify my ComputerInventory model?

1
0
12/6/2012 2:20:26 PM

Accepted Answer

Visit my post to learn more about how EF's navigation properties operate. This explains how to set up foreign key properties using the model builder. http://blog.staticvoid.co.nz/2012/07/entity-framework-navigation-property.html

2
12/6/2012 6:55:51 PM

Popular Answer

Statistical Notes

[ForeignKey("LocationId")]
public virtual Location { get; set; }

I often use data annotations, but I really need to start looking at the Fluent API stuff and keep my POCOs free of annotations going ahead.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow