实体框架数据库首先是多对多的

时间:2021-10-18 07:41:59

I've created an Entity Framework model from the database. I have many-to-many relationship: User - UserRole - Role.

我从数据库中创建了一个Entity Framework模型。我有多对多的关系:用户 - UserRole - 角色。

EF created UserRole entity and UserRoles navigation property in the User entity and in Role entity, but I'd rather have Roles in User and Users in Role. Is that possible to be designed through the Model Designer? How can I configure manually many-to-many relationship with a table in the middle?

EF在User实体和Role实体中创建了UserRole实体和UserRoles导航属性,但我宁愿在用户和角色中拥有角色。这是否可以通过模型设计师设计?如何手动配置中间表的多对多关系?

1 个解决方案

#1


6  

EF normally creates the intermediate model, if the UserRole table comprises of columns other than foreign keys for User and Role table.

如果UserRole表包含除User和Role表的外键之外的列,则EF通常会创建中间模型。

So if you had just 2 columns in the UserRoles table, both FKs to the User and Role tables (not even a surrogate key), EF would create the Model as you wanted. (without any intermediate model) So that is one way to go, for automatic generation of the desired behavior. Have just 2 columns in the table.

因此,如果您在UserRoles表中只有2列,对于用户和角色表都有FK(甚至不是代理键),EF将根据需要创建模型。 (没有任何中间模型)因此,这是一种方法,可以自动生成所需的行为。表中只有2列。

But if you have other non-key columns (data) in this table, then what EF is doing is correct. You need the intermediate entity.

但是如果你在这个表中有其他非键列(数据),那么EF正在做什么是正确的。你需要中间实体。

And in the case where you don't have any non-key columns, don't want to modify your DB anymore and don't need this middle table in your model, you could manually modify the OnModelCreating, to specify the Many-to-Many and hide the intermediate table.

如果您没有任何非键列,不再需要修改数据库,并且不需要在模型中使用此中间表,则可以手动修改OnModelCreating,以指定Many-to - 许多并隐藏中间表。

Here are all the steps:

以下是所有步骤:

  1. Remove the intermediate table definition C# class from the model layer, and its references in DbContext and User and Role classes.
  2. 从模型层中删除中间表定义C#类,并在DbContext和User和Role类中删除它们。
  3. Add a virtual Collection property in both User and Role class, for each other.
  4. 在User和Role类中为彼此添加虚拟Collection属性。

e.g. in the User class,

例如在User类中,

public virtual ICollection<Role> Roles { get; set; }

and in the User constructor

并在User构造函数中

this.Roles = new HashSet<Role>();

// on the OnModelCreating method, add this snippet
modelBuilder.Entity<User>().HasMany<Role>(u => u.Roles)
                          .WithMany(r => r.Users)
                          .Map(ru => 
                                   {  
                                     ru.MapLeftKey("UserId");        
                                     ru.MapRightKey("RoleId"); 
                                     ru.ToTable("UserRole"); 
                                   });

#1


6  

EF normally creates the intermediate model, if the UserRole table comprises of columns other than foreign keys for User and Role table.

如果UserRole表包含除User和Role表的外键之外的列,则EF通常会创建中间模型。

So if you had just 2 columns in the UserRoles table, both FKs to the User and Role tables (not even a surrogate key), EF would create the Model as you wanted. (without any intermediate model) So that is one way to go, for automatic generation of the desired behavior. Have just 2 columns in the table.

因此,如果您在UserRoles表中只有2列,对于用户和角色表都有FK(甚至不是代理键),EF将根据需要创建模型。 (没有任何中间模型)因此,这是一种方法,可以自动生成所需的行为。表中只有2列。

But if you have other non-key columns (data) in this table, then what EF is doing is correct. You need the intermediate entity.

但是如果你在这个表中有其他非键列(数据),那么EF正在做什么是正确的。你需要中间实体。

And in the case where you don't have any non-key columns, don't want to modify your DB anymore and don't need this middle table in your model, you could manually modify the OnModelCreating, to specify the Many-to-Many and hide the intermediate table.

如果您没有任何非键列,不再需要修改数据库,并且不需要在模型中使用此中间表,则可以手动修改OnModelCreating,以指定Many-to - 许多并隐藏中间表。

Here are all the steps:

以下是所有步骤:

  1. Remove the intermediate table definition C# class from the model layer, and its references in DbContext and User and Role classes.
  2. 从模型层中删除中间表定义C#类,并在DbContext和User和Role类中删除它们。
  3. Add a virtual Collection property in both User and Role class, for each other.
  4. 在User和Role类中为彼此添加虚拟Collection属性。

e.g. in the User class,

例如在User类中,

public virtual ICollection<Role> Roles { get; set; }

and in the User constructor

并在User构造函数中

this.Roles = new HashSet<Role>();

// on the OnModelCreating method, add this snippet
modelBuilder.Entity<User>().HasMany<Role>(u => u.Roles)
                          .WithMany(r => r.Users)
                          .Map(ru => 
                                   {  
                                     ru.MapLeftKey("UserId");        
                                     ru.MapRightKey("RoleId"); 
                                     ru.ToTable("UserRole"); 
                                   });