Fluent API 多对多映射错误

发布于 2024-12-13 12:09:07 字数 1026 浏览 3 评论 0原文

我有以下映射来支持多对多表(User_Role),

     modelBuilder.Entity<Role>()
        .HasMany<User>(u => u.users)
        .WithMany(r => r.roles)
        .Map(m => 
           m.MapLeftKey("role_id")
           m.MapRightKey("user_id")
           m.ToTable("User_Role"));

这对于将许多角色映射到一个用户以及将许多用户映射到一个角色非常有用。我遇到的问题是当我需要通过实体向表 User_Role 添加新行时,如下所示:

public class User_Role
{
   [Key Column(Order=1)]   
   public int role_id {get;set;)
   [Key Column(Order=1)]
   public int user_id {get;set;)
}

每当我尝试按如下方式访问该实体时:

 dbContext.User_Role.Add(new User_Role {user_id ....

EF 查找一个名为 User_Role1 的不存在的表...它添加一个 ' 1' 到表名。

然后我尝试添加:

  [Table("User_Role")] 

这负责添加“1”,但我现在收到此错误:

“带有架构“dbo”和表“User_Role”的实体集“RoleUser”已定义。每个实体集必须引用唯一的架构和表”

我能够确认以下几行一起导致了问题,但我有点需要它们

 m.ToTable("User_Role") and public class User_Role..

任何建议都会很棒......我被难住了。

I have the following mapping to support a many to many table (User_Role)

     modelBuilder.Entity<Role>()
        .HasMany<User>(u => u.users)
        .WithMany(r => r.roles)
        .Map(m => 
           m.MapLeftKey("role_id")
           m.MapRightKey("user_id")
           m.ToTable("User_Role"));

This works great from mapping many roles to a user and many users to a role. The problem I am having is when I need to add a new row to the table User_Role via an entity as follows:

public class User_Role
{
   [Key Column(Order=1)]   
   public int role_id {get;set;)
   [Key Column(Order=1)]
   public int user_id {get;set;)
}

Whenever I try to access this entity as follows:

 dbContext.User_Role.Add(new User_Role {user_id ....

EF looks for a non existent table called User_Role1 ... its adding a '1' to the table name.

I then tried to add:

  [Table("User_Role")] 

This takes care of the adding a '1' but I now get this error:

"The EntitySet 'RoleUser' with schema 'dbo' and table 'User_Role' was already defined. Each EntitySet must refer to a unique schema and table"

I was able to confirm that the following lines together are causing the problem but I kind of need them both

 m.ToTable("User_Role") and public class User_Role..

Any suggestions would be great...I am stumped.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱你一生 2024-12-20 12:09:07

您无法通过模型中的实体类来表示多对多关系中的联接表。该连接表由 EF 管理,您无法直接访问该表。如果要在现有用户和现有角色之间创建关系,则必须使用这两个实体来执行此操作:

var user = dbContext.Users.Single(u => u.id == user_id);
var role = dbContext.Roles.Single(r => r.id == role_id);

// if you don't have lazy loading and don't instantiate the collection
// in the constructor, add this: user.roles = new List<Role>(); 

user.roles.Add(role);

dbContext.SaveChanges();

这会将新关系((user_id,role_id)对)的条目写入连接表中。

编辑

如果您手头只有两个关键属性并且不想从数据库加载用户和角色,您可以使用附加的“存根实体”:

var user = new User { id = user_id, roles = new List<Role>() };
var role = new Role { id = role_id };

dbContext.Users.Attach(user);
dbContext.Roles.Attach(role);

user.roles.Add(role);

dbContext.SaveChanges();

You cannot represent the join table in a many-to-many relationship by an entity class in your model. This join table is managed by EF and you cannot directly access this table. If you want to create a relationship between an existing user and an existing role you must do this using these two entities:

var user = dbContext.Users.Single(u => u.id == user_id);
var role = dbContext.Roles.Single(r => r.id == role_id);

// if you don't have lazy loading and don't instantiate the collection
// in the constructor, add this: user.roles = new List<Role>(); 

user.roles.Add(role);

dbContext.SaveChanges();

This will write an entry for the new relationship (the (user_id,role_id) pair) into the join table.

Edit

If you only have the two key properties at hand and don't want to load the user and role from the database you can work with attached "stub entities":

var user = new User { id = user_id, roles = new List<Role>() };
var role = new Role { id = role_id };

dbContext.Users.Attach(user);
dbContext.Roles.Attach(role);

user.roles.Add(role);

dbContext.SaveChanges();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文