简体   繁体   English

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

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

Database Structure数据库结构
I've got a very denormalized SQL table with a structure like this:我有一个非常非规范化的 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
)

While each particular record is unique, one shipment can have multiple pallets going to multiple destinations.虽然每个特定记录都是唯一的,但一次装运可以有多个托盘前往多个目的地。

.NET Interface .NET 接口
This is going to be manipulated by EF objects, which I want to structure like this:这将由 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;}
}

The Problem问题
While I've found tutorials on splitting tables into one-to-one entities and on mapping foreign-keyed data to collections in EF, I can't find anything about mapping columns from one table into collections.虽然我找到了有关将表拆分为一对一实体以及将外键数据映射到 EF 中的集合的教程,但我找不到有关将列从一个表映射到集合的任何内容。 Is this possible, or am I limited to splitting the table, creating views, or creating a POCO class with a property for each column?这是可能的,还是我仅限于拆分表、创建视图或为每列创建一个具有属性的 POCO 类?

Endmatter终结者
Another application will access the SQL table to generate reports on an arbitrary number of shipments, so the Powers That Be chose to use a denormalized table for performance's sake, rather than a suite of normalized tables and a view, which would take longer to retrieve.另一个应用程序将访问 SQL 表以生成关于任意数量货物的报告,因此 Powers That Be 出于性能考虑选择使用非规范化表,而不是一套规范化表和视图,因为后者需要更长的时间来检索。

Your classes should look something link this你的课程应该看起来链接这个

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