簡體   English   中英

如何在實體框架中鏈接表

[英]How to link tables in Entity Framework

我正在嘗試使用MVC和Entity Framework創建資產跟蹤系統。 該數據庫已經存在。 我有一個ComputerInventory表,其中包含一個Location字段,這是Location表的LocationID的偽造鍵。 我想做的是當我輸出有關庫存的信息時,我不必看到LocationID,而要查看對應的LocationID的Location字段。

我一直在使用一些tutproials,但似乎無法根據自己的需要進行更改。 我認為這很普遍,所以也許我錯過了一些東西。 這是我的計算機清單:

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

這是我的位置模型

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

這是我試圖在視圖中輸出數據的位置:

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

}

我必須更改ComputerInventory模型還是必須從視圖訪問位置?

資料注解

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

我通常使用數據注釋,但是應該開始研究Fluent API,並保持POCO清潔注釋

查看我有關EF中導航屬性如何工作的文章。 這將介紹如何使用模型構建器來配置外鍵屬性。 http://blog.staticvoid.co.nz/2012/07/entity-framework-navigation-property.html

嘗試在實體框架中使用一對多關系 ,這可以幫助您如何在兩個表的關系中使用表及其字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM