同样的EntityFramework表多对多的关系 [英] EntityFramework Same Table Many to Many Relationship
问题描述
我有一个名为Products表这显然包含产品。
但是,我需要建立相关产品。所以我所做的就是创建一个名为product_related它有两个的PK结合表。从产品ID产品表和RelatedID也是从产品表。
I have a table called Products which obviously contains products. However, I need to create related products. So what I've done is create a junction table called product_related which has two PKs. ProductID from Products table and RelatedID also from Products table.
我已经使用EF并建立了一切其他表。我应该如何添加这个正确,以创建以产品为这样的关系: product.Products.Add(这里的产品对象)
。当然,这里产品
重新present产品对象,我已经从使用DB牵强 db.Products.FirstOr ...
。
I already use EF and have set up everything on other tables. How should I add this properly in order to create a relationship with products as such:
product.Products.Add(product object here)
. Of course here product
represent a product object that I've fetched from the db using db.Products.FirstOr...
.
我应该如何做到这正常吗?多对多的同桌?
How should I do this properly ? A many to many to the same table?
感谢。
推荐答案
为了创建具有多对一对多关系的数据库-First方法您需要设置的数据库架构遵循一定规则:
In order to create a many-to-many relationship with Database-First approach you need to setup a database schema that follows certain rules:
- 创建一个
产品
表列的ProductID
作为主键 - 与列创建一个
ProductRelations
表的ProductID
和列RelatedID
和标记两列作为主键(复合键) - 请勿使用任何其他列添加到
ProductRelations
表。这两个键列必须在表中的列只有让EF承认此表为纽带表一个多一对多的关系 - 创建两个外键的两个表之间的关系:
- 第一个关系有
产品
表作为主键表与的ProductID
作为主键和ProductRelations
表作为外键表是仅的ProductID
为外键 - 第二个关系也有
产品
表作为主键表与的ProductID
作为主键,在ProductRelations
表作为外键表是仅RelatedID
为外键
- Create a
Products
table with a columnProductID
as primary key - Create a
ProductRelations
table with a columnProductID
and a columnRelatedID
and mark both columns as primary key (composite key) - Don't add any other column to the
ProductRelations
table. The two key columns must be the only columns in the table to let EF recognize this table as a link table for a many-to-many relationship - Create two foreign key relationships between the two tables:
- The first relationship has the
Products
table as primary-key-table with theProductID
as primary key and theProductRelations
table as foreign-key-table with only theProductID
as foreign key - The second relationship also has the
Products
table as primary-key-table with theProductID
as primary key and theProductRelations
table as foreign-key-table with only theRelatedID
as foreign key
如果您生成来自这两个表的实体数据模型现在你会得到的只有一个实体,即
产品
实体(或者产品
如果禁用单数)。链接表ProductRelations
不会被暴露为一个实体。If you generate an entity data model from those two tables now you will get only one entity, namely a
Product
entity (or maybeProducts
if you disable singularization). The link tableProductRelations
won't be exposed as an entity.的
产品
实体将拥有的两个导航性能public EntityCollection<Product> Products { get { ... } set { ... } } public EntityCollection<Product> Products1 { get { ... } set { ... } }
这些导航集合是相同的多对多关系的两个端点。 (如果你有两个不同的表,你希望通过一个多一对多的关系联系起来,说表
A
和B
,一个导航集(烧烤
)将在实体A
和其他(作为
)将在实体B
。但是因为你的关系是自我参照两个导航属性在实体产品
)These navigation collections are the two endpoints of the same many-to-many relationship. (If you had two different tables you wanted to link by a many-to-many relationship, say table
A
andB
, one navigation collection (Bs
) would be in entityA
and the other (As
) would be in entityB
. But because your relationship is "self-referencing" both navigation properties are in entityProduct
.)这两个属性的含义是:
产品
是与给定产品的产品,产品1
是引用该给定产品的产品。例如:如果关系意味着产品需要的其他产品为制造零件和你的产品笔记本电脑,处理器,硅芯片,那么处理器是的由 硅片(硅芯片是一个元素产品
的处理器
产品实体的集合),是按笔记本电脑使用的(记事本是的
产品实体)。取而代之的产品1
集合中的元素处理器产品
和产品1
名称MadeOf
和UsedBy
会更合适呢。The meaning of the two properties are:
Products
are the products related to the given product,Products1
are the products that refer to the given product. For example: If the relationship means that a product needs other products as parts to be manufactured and you have the products "Notebook", "Processor", "Silicon chips" then the "Processor" is made of "Silicon chips" ("Silicon chips" is an element in theProducts
collection of theProcessor
product entity) and is used by a "Notebook" ("Notebook" is an element in theProducts1
collection of theProcessor
product entity). Instead ofProducts
andProducts1
the namesMadeOf
andUsedBy
would be more appropriate then.您可以安全地从生成的模型中删除的收藏品之一,如果你只在关系的一侧感兴趣。只要删除,例如
产品1
模型设计器表面。您还可以重命名的属性。的关系仍然会多到多You can safely delete one of the collections from the generated model if you are only interested in one side of the relationship. Just delete for example
Products1
in the model designer surface. You can also rename the properties. The relationship will still be many-to-many.修改
至于问评论模型和映射了 code-First方法是:
As asked in a comment the model and mapping with a Code-First approach would be:
型号:
public class Product { public int ProductID { get; set; } public ICollection<Product> RelatedProducts { get; set; } }
映射:
public class MyContext : DbContext { public DbSet<Product> Products { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Product>() .HasMany(p => RelatedProducts) .WithMany() .Map(m => { m.MapLeftKey("ProductID"); m.MapRightKey("RelatedID"); m.ToTable("product_related"); }); } }
这篇关于同样的EntityFramework表多对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- The first relationship has the
- 第一个关系有