C# 如何使用实体框架 fluent API 配置多对多关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8927278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:14:23  来源:igfitidea点击:

How to configure many to many relationship using entity framework fluent API

c#entity-framework-4.1ef-code-firstassociations

提问by Fixer

I'm trying to set up a many to many relationship in EF code first but the default conventions is getting it wrong. The following classes describes the relationship:

我首先尝试在 EF 代码中设置多对多关系,但默认约定是错误的。以下类描述了这种关系:

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:

这看起来不像是多对多的表结构?如何配置 fluent API 以反映关系并创建中间表:

AccountProducts Table
---------------------
Account_Id
Product_Id

采纳答案by Slauma

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

回答by NinjaNye

What EF has suggested is a one-to-many relationship.

EF 建议的是一对多关系。

One Account can have many Products, i.e. each Product has an Account_Id

一个Account可以有多个Products,即每个Product有一个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; }
}

回答by archil

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.

Products 表应该为它的 Account 存储密钥,就像它现在所做的那样。

What you are trying to see in db, is many-to-many relationship, not one to many. If you want to achieve that with code first, you should redesign your entities

您试图在 db 中看到的是多对多关系,而不是一对多关系。如果你想先用代码实现这一点,你应该重新设计你的实体

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.

在这种情况下,一个产品可以有多个账户,一个账户可以有多个产品。

回答by andre

        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"));
    }