Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 可以将表拆分为具有一对多关系的EF类吗?_C#_.net_Entity Framework_Table Splitting - Fatal编程技术网

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

C# 可以将表拆分为具有一对多关系的EF类吗?,c#,.net,entity-framework,table-splitting,C#,.net,Entity Framework,Table Splitting,数据库结构 我得到了一个非常非规范化的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_de

数据库结构
我得到了一个非常非规范化的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
)
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;}
}
虽然每个特定记录都是唯一的,但一次装运可以有多个托盘到达多个目的地

.NET接口
这将由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
)
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;}
}
类ShippingContext:DbContext
{
公共虚拟数据库集{get;set;}
}
分类装运
{
int ShipmentId{get;set;}
列表ShipmentStops{get;set;}
}
班级目的地
{
字符串DestinationId{get;set;}
int DestinationOrder{get;set;}
列出托盘{get;set;}
}
类托盘
{
int PalletId{get;set;}
字符串托盘描述{get;set;}
}
问题
虽然我找到了关于将表拆分为一对一实体以及将外键数据映射到EF中集合的教程,但我找不到任何关于将列从一个表映射到集合的内容。这是可能的,还是仅限于拆分表、创建视图或创建具有每列属性的POCO类

终结者

另一个应用程序将访问SQL表以生成关于任意数量装运的报告,因此为了性能起见,可以选择使用非规范化表,而不是一组规范化表和视图,这将花费更长的时间来检索。

您的类应该在这一链接中查找某些内容

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
}
公共类ShipmnetContext:DbContext
{
公共数据库集{get;set;}
公共数据库集目的地{get;set;}
公共数据库集托盘{get;set;}
}
公共舱位装运
{
public int ShipmentId{get;set;}
公共ICollection ShipmentStops{get;set;}
公共运输()
{
ShipmentStops=newhashset();
}
}
公务舱目的地
{
[关键]
公共字符串DestinationId{get;set;}
public int DestinationOrder{get;set;}
//[必需]
公共装运装运{get;set;}//装运表的外键,通过添加[Required]属性使属性NotNull
公共ICollection托盘{get;set;}
公共目的地()
{
托盘=新HashSet();
}
}
公营货盘
{
公共int PalletId{get;set;}
公共字符串PalletDescription{get;set;}
公共目标{get;set;}//目标表的外键
}

Mulțumesc mult!一个人要怎么给其中一个写信?