DFord
DFord

Reputation: 2358

How to link tables in Entity Framework

I am trying to create an asset tracking system using MVC and Entity Framework. The database already exists. I have a ComputerInventory table with a field of Location which is a forgein key to the the LocationID of the Location table. What im trying to do is when i output the information about the inventory, i dont have to see the LocationID, I want to see the Location field of the corrisponding LocationID.

I have been using some tutproials but I can't seem to change it to suite my needs. I would think that this is pretty common so maybe i'm missing something. Here is my ComputerInventory:

[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 Location model

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

Here is the where I am trying to output the data in the view:

@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>

}

Do I have to change my ComputerInventory model or do I have to access the Location from the view?

Upvotes: 0

Views: 20574

Answers (3)

undefined
undefined

Reputation: 34248

Check out my article on how navigation properties in EF work. This goes through how to use the model builder to configure foreign key properties. http://blog.staticvoid.co.nz/2012/07/entity-framework-navigation-property.html

Upvotes: 2

zybroxz
zybroxz

Reputation: 713

Data Annotations

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

I generally use the data annotations, however should start looking at the Fluent API stuff moving forward and keep my POCO's clean of annotations

Upvotes: 1

Sagar Upadhyay
Sagar Upadhyay

Reputation: 819

Try to using One to many relation in entity framework, this can help you how can we use table and its field in relationship, of two table.

Upvotes: -1

Related Questions