2016-09-15 81 views
2

數據庫結構
我有一個非常非規範化SQL表,像這樣的結構:一張表可以通過一對多關係分割成EF類嗎?

CREATE TABLE logistix.shipments 
(
    shipment_id INT NOT NULL PRIMARY KEY, 
    destination_id NVARCHAR(15) NOT NULL PRIMARY KEY, 
    pallet_id INT NOT NULL PRIMARY KEY, 
    destination_order INT NOT NULL, 
    pallet_description NVARCHAR(40) NOT NULL 
) 

雖然每個特定的記錄是唯一的,一個貨可以有多個貨盤將多個目的地。

.NET接口
這將是由EF對象,我想構建這樣來操作:

class ShippingContext : DbContext 
{ 
     public virtual DbSet<Shipment> Shipments {get; set;} 
} 

class Shipment 
{ 
    int ShipmentId {get; set;} 
    List<Destination> ShipmentStops {get; set;} 
} 

class Destination 
{ 
    string DestinationId {get; set;} 
    int DestinationOrder {get; set;} 
    List<Pallet> Pallets {get; set;} 
} 

class Pallet 
{ 
    int PalletId {get; set;} 
    string PalletDescription {get; set;} 
} 

的問題
雖然我發現在分裂教程表格轉換爲一對一實體,並將外鍵控制的數據映射到EF中的集合,但我無法找到任何有關將一個表格中的列映射到集合中的任何內容。這是可能的,還是僅限於分割表格,創建視圖或爲每列創建帶有屬性的POCO類?

Endmatter
另一個應用程序將訪問SQL表產生出貨量任意數量的報告,使被選擇使用非規範化表性能的緣故,而不是一套規範化的表和的權力查看,這將需要更長的時間來檢索。

回答

2

你的類看起來應該鏈接該

public class ShipmnetContext : DbContext 
{ 
    public DbSet<Shipment> Shipments { get; set; } 
    public DbSet<Destination> Destinations { get; set; } 
    public DbSet<Pallet> Pallets { get; set; } 
} 

public class Shipment 
{ 
    public int ShipmentId { get; set; } 
    public ICollection<Destination> ShipmentStops { get; set; } 

    public Shipment() 
    { 
     ShipmentStops = new HashSet<Destination>(); 
    } 
} 

public class Destination 
{ 
    [Key] 
    public string DestinationId { get; set; } 
    public int DestinationOrder { get; set; } 
    //[Required] 
    public Shipment Shipment { get; set; } //Foreign key to Shipment table, make property NotNull by adding [Required] attribute 
    public ICollection<Pallet> Pallets { get; set; } 

    public Destination() 
    { 
     Pallets = new HashSet<Pallet>(); 
    } 
} 

public class Pallet 
{ 
    public int PalletId { get; set; } 
    public string PalletDescription { get; set; } 
    public Destination Destination { get; set; } //Foreign key to Destination table 
} 
+0

MulţumescMULT!如何寫一個這樣的? – JAF

相關問題