繁体   English   中英

可以将表拆分为具有一对多关系的 EF 类吗?

[英]Can a Table Be Split Into EF Classes With a One-To-Many Relationship?

数据库结构
我有一个非常非规范化的 SQL 表,其结构如下:

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 类?

终结者
另一个应用程序将访问 SQL 表以生成关于任意数量货物的报告,因此 Powers That Be 出于性能考虑选择使用非规范化表,而不是一套规范化表和视图,因为后者需要更长的时间来检索。

你的课程应该看起来链接这个

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM