Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
Entity framework 实体框架多对多实体?_Entity Framework - Fatal编程技术网

Entity framework 实体框架多对多实体?

Entity framework 实体框架多对多实体?,entity-framework,Entity Framework,这不是关于如何创建多对多关系的另一个问题,而是如何实际控制映射关系的实体 例如。。。表产品和表供应商之间的多对多关系 我需要一个SupplierProduct,它具有特定于产品+供应商组合的列 class SupplierProduct { public int SupplierId { get; set; } public int ProductId { get; set; } // additional properties specific to a S+P co

这不是关于如何创建多对多关系的另一个问题,而是如何实际控制映射关系的实体

例如。。。表产品和表供应商之间的多对多关系

我需要一个SupplierProduct,它具有特定于产品+供应商组合的列

class SupplierProduct {
    public int SupplierId { get; set; }
    public int ProductId { get; set; }

    // additional properties specific to a S+P combo: 
    public bool IsProductAssembled { get; set; }
}

有些供应商组装,有些则不组装。如果我有这样一个类,我如何使用它来代替它创建的默认EF多对多表

使用您的
供应商产品
类作为关系:

class SupplierProduct {
    public int SupplierId { get; set; }
    public int ProductId { get; set; }

    // additional properties specific to a S+P combo: 
    public bool IsProductAssembled { get; set; }
}

class Product {
    // ... lot of properties

    // Link all the suppliers of this products
    public IList<SupplierProduct> Suppliers { get; set; }
}    

class Supplier {
    // ... lot of properties

    // Link all the product this supplier supplies
    public IList<SupplierProduct> Products { get; set; }
}

我不确定EF能做到这一点。那么,由于映射表仅用于将实体连接在一起,您将如何访问
IsProductAssembled
属性。然而,对于您正在使用的
SupplierProduct
类,您不能只配置两个一对多关系吗?一个
产品
可以有多个
供应商产品
,一个
供应商
可以有多个
供应商产品
。这是一种不同的思维方式。听起来不错。我试试看。需要明确的是:您的意思是完全删除映射表(如从代码优先类中删除集合,以便EF不会自动创建映射表。)?是的,我的回答澄清了这一部分。这种情况经常发生。你有一个多对多的关系,然后你意识到这种关系有一个属性,你必须将其提升为一个独立的实体。请你延长你的回答好吗?特别是这一部分:
然后,配置产品为有很多供应商,供应商为有很多产品。产品和供应商之间不再有直接关系。
。以及如何定义供应商产品。
modelBuilder.Entity<Product>()
    .HasMany<SupplierProduct>(p => p.Suppliers)
    .WithRequired();

modelBuilder.Entity<Supplier>()
    .HasMany<SupplierProduct>(s => s.Products)
    .WithRequired();