同样的EntityFramework表多对多的关系 [英] EntityFramework Same Table Many to Many Relationship

查看:130
本文介绍了同样的EntityFramework表多对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为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 column ProductID as primary key
    • Create a ProductRelations table with a column ProductID and a column RelatedID 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 the ProductID as primary key and the ProductRelations table as foreign-key-table with only the ProductID as foreign key
      • The second relationship also has the Products table as primary-key-table with the ProductID as primary key and the ProductRelations table as foreign-key-table with only the RelatedID 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 maybe Products if you disable singularization). The link table ProductRelations 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 and B, one navigation collection (Bs) would be in entity A and the other (As) would be in entity B. But because your relationship is "self-referencing" both navigation properties are in entity Product.)

      这两个属性的含义是:产品是与给定产品的产品,产品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 the Products collection of the Processor product entity) and is used by a "Notebook" ("Notebook" is an element in the Products1 collection of the Processor product entity). Instead of Products and Products1 the names MadeOf and UsedBy 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆