实体框架多对多不断添加额外记录

发布于 2024-10-31 14:23:50 字数 1214 浏览 1 评论 0原文

我尝试首先使用实体​​框架代码将新记录添加到链接表中。我拥有的是 UserRole 上的多对多。我遇到的情况是,当我更改用户的角色时,我删除他们以前的所有角色并添加新角色,如下所示:

//Delete all associated roles for user
var roleUser = db.Users.Include(r => r.Roles).FirstOrDefault(u => u.UserId == user.UserId);
var usersRoles = roleUser.Roles;

usersRoles.ForEach(role => roleUser.Roles.Remove(role));

//add the new roles
roleUser.Roles.AddRange(detachedUser.Roles);

db.SaveChanges();

因此它完美地删除了它们。但是当添加新角色时,它不仅将其添加到链接表中,而且还添加到Role表中。添加一个全新的角色,没有 RoleNameuser.Roles 将包含一个具有以下数据的项目:

RoleId;//1 <-- this Id exists in the database already but yet still it creates one instead of a linktable record.
RoleName;//null

如何防止 EF 添加全新记录并仅向链接表添加一条记录?

更新:我最终这样做了:

var roleUser = db.Users.Include(r => r.Roles).FirstOrDefault(u => u.UserId == user.UserId);
var roles = db.Roles;

foreach (var role in roles)
{
     if (user.Roles.Any(r => r.RoleId == role.RoleId))
     {
          roleUser.Roles.Add(role);
     }
     else
     {
         roleUser.Roles.Remove(role);
     }
}
db.SaveChanges();

I'm trying to add a new record to the link table using entityframework code first. What I have is a many to many on User and Role. The scenario I have is that when I'm changing a role for the user, I delete all their previous roles and add the new roles as follows:

//Delete all associated roles for user
var roleUser = db.Users.Include(r => r.Roles).FirstOrDefault(u => u.UserId == user.UserId);
var usersRoles = roleUser.Roles;

usersRoles.ForEach(role => roleUser.Roles.Remove(role));

//add the new roles
roleUser.Roles.AddRange(detachedUser.Roles);

db.SaveChanges();

So it removes them perfectly. But when adding new roles, it doesn't only add it to the link table but also the Role table. A completely new role gets added without a RoleName. user.Roles would contain an item with the following data:

RoleId;//1 <-- this Id exists in the database already but yet still it creates one instead of a linktable record.
RoleName;//null

How do I prevent EF from adding a whole new record and just add a record to the link table?

Update: I ended up doing this:

var roleUser = db.Users.Include(r => r.Roles).FirstOrDefault(u => u.UserId == user.UserId);
var roles = db.Roles;

foreach (var role in roles)
{
     if (user.Roles.Any(r => r.RoleId == role.RoleId))
     {
          roleUser.Roles.Add(role);
     }
     else
     {
         roleUser.Roles.Remove(role);
     }
}
db.SaveChanges();

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

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

发布评论

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

评论(3

爱已欠费 2024-11-07 14:23:51

在向用户添加新角色之前尝试保存

//Delete all associated roles for user 
var roleUser = db.Users.Include(r => r.Roles).FirstOrDefault(u => u.UserId == user.UserId); 
var usersRoles = roleUser.Roles; 

usersRoles.ForEach(role => roleUser.Roles.Remove(role));  
db.SaveChanges(); 

roleUser.Roles.AddRange(user.Roles); //add the new roles 
db.SaveChanges(); 

编辑:

看看这个:

roleUser.Roles.AddRange(user.Roles); //add the new roles 

user.Roles 来自哪里?

try to save before adding new roles to the user

//Delete all associated roles for user 
var roleUser = db.Users.Include(r => r.Roles).FirstOrDefault(u => u.UserId == user.UserId); 
var usersRoles = roleUser.Roles; 

usersRoles.ForEach(role => roleUser.Roles.Remove(role));  
db.SaveChanges(); 

roleUser.Roles.AddRange(user.Roles); //add the new roles 
db.SaveChanges(); 

Edit:

Have a look at this :

roleUser.Roles.AddRange(user.Roles); //add the new roles 

where do user.Roles come from ?

断舍离 2024-11-07 14:23:51

我认为您遇到的问题是您需要在将角色添加到用户之前附加角色。

您也不需要将它们全部删除,只需删除那些已删除的,然后只添加新的。

I think the problem you have is that you need to attach your Roles before adding them to the user.

you also don't need to remove them all, simply remove those removed and then only add the new ones.

你的笑 2024-11-07 14:23:51

对于您添加的每个卷,您可以尝试检查或设置实体状态。例如,这里我将集合设置为未更改。这样它就不会尝试插入或更新它。

myContext.Entry(roll).State = EntityState.Unchanged

更多信息请点击这里:
http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add -attach-and-entity-states.aspx

For each roll that you add you could try checking or setting the entity state. For example here I set the set to unchaged. This way it wont attempt to insert or update it.

myContext.Entry(roll).State = EntityState.Unchanged

More info here:
http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx

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