简体   繁体   English

如何使用实体框架流利的API配置多对多关系

[英]How to configure many to many relationship using entity framework fluent API

I'm trying to set up a many to many relationship in EF code first but the default conventions is getting it wrong. 我正在尝试首先在EF代码中建立多对多关系,但是默认的约定弄错了。 The following classes describes the relationship: 以下类描述了这种关系:

class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class Account
{        
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}

One Account can have many Products. 一个帐户可以有多个产品。

However the EF conventions will create the DB tables as: 但是,EF约定将按以下方式创建数据库表:

Products Table
--------------
Id
Name
Account_Id  <- What is this?

Accounts Table
--------------
Id
Name

This doesn't look like a many-to-many table structure? 这看起来不像多对多表结构吗? How can i get configure the fluent API to reflect the relationship and create an intermediary table: 我如何配置流利的API以反映关系并创建中间表:

AccountProducts Table
---------------------
Account_Id
Product_Id
modelBuilder.Entity<Account>()
            .HasMany(a => a.Products)
            .WithMany()
            .Map(x =>
            {
                x.MapLeftKey("Account_Id");
                x.MapRightKey("Product_Id");
                x.ToTable("AccountProducts");
            });

What EF has suggested is a one-to-many relationship. 英孚提出的是一对多关系。

One Account can have many Products, ie each Product has an Account_Id 一个帐户可以有多个产品,即每个产品都有一个Account_Id

If you want a many to many relationship (and create an intermediary table), the following should work 如果您想要多对多关系(并创建一个中间表),则以下方法应该有效

class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Account> Accounts { get; set; }
}

class Account
{        
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}

Code first is creating tables in right relational way. 代码首先是以正确的关系方式创建表。 When 什么时候

One Account can have many Products. 一个帐户可以有多个产品。

Products table should store key for its Account, as it actually does now. 产品表应该像现在一样存储其帐户的密钥。

What you are trying to see in db, is many-to-many relationship, not one to many. 您试图在db中看到的是多对多关系,而不是一对多关系。 If you want to achieve that with code first, you should redesign your entities 如果要首先使用代码来实现,则应重新设计实体

class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Account> Accounts { get; set; }
}

class Account
{        
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}

In this case, one product can have many accounts, and one account can have many products. 在这种情况下,一个产品可以有多个帐户,而一个帐户可以有许多产品。

        public AccountProductsMap()
    {
        this.ToTable("AccountProducts");
        this.HasKey(cr => cr.Id);

        this.HasMany(cr => cr.Account)
            .WithMany(c => c.Products)
            .Map(m => m.ToTable("AccountProducts_Mapping"));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM