更新具有一对多关系的实体
我遇到了存在一对多关系的问题,并且我正在尝试更新其中一个表和链接表中的数据。
- 我有两个表(用户、角色)和一个链接表(UserRoles)。
- 一个用户有多个角色。
- 角色是预定义的,只有 3 行,如下所示。
我想用角色更新用户。我无法使用以下代码。 请分享您的意见。
Original
User : John
Roles : Admin, User
Update to
User : John
Roles : User
这是表结构
User
UserID : Pk
Name
Roles //Data is not added in this table, Data already exists in this table
RoleID : Pk
Name
RoleID Name
1 Admin
2 User
3 None
UserRoles
UserID : PK
RoleID : Pk
相应的实体
User
UserId
Name
EntityCollection<Role> Roles
Role
RoleId
Name
EntityCollection<User> Users
我已经尝试使用以下代码
public void update(int userId, string newusername, list<int> roleList)
{
using (DBEntites context = new DBEntites())
{
User objUserInDb = new User();
objUserInDb.UserID = userId;
Context.Users.Attach(objUserInDb);
objUserInDb.Name = newusername;
objUserInDb.Roles.Clear();
// TRIED TO USE The remove method objUserInDb.Roles.Remove(entity),
// is returning false
foreach (long pkIDToAdd in roleList)
{
Role objRoleInDb = new Role();
objRoleInDb.RoleId = pkIDToAdd;
// GIVES EXCEPTION
//An object with the same key already exists in the ObjectStateManager.
//The ObjectStateManager cannot track multiple objects with the same
//
context.Roles.Attach(objRoleInDb);
objUserInDb.Roles.Add(objRoleInDb);
}
}
}
I am running into problem where there is one-to-many relationship and I am trying to update the data in one of the tables and the link table.
- I have two tables (User, Roles) and one link table (UserRoles).
- A User has many Roles.
- The Roles are predefined and there are only 3 rows as shown below.
I want to update the User with Roles. I am unable to do with the following code.
Please share your inputs.
Original
User : John
Roles : Admin, User
Update to
User : John
Roles : User
This is the table structure
User
UserID : Pk
Name
Roles //Data is not added in this table, Data already exists in this table
RoleID : Pk
Name
RoleID Name
1 Admin
2 User
3 None
UserRoles
UserID : PK
RoleID : Pk
The corresponding entities
User
UserId
Name
EntityCollection<Role> Roles
Role
RoleId
Name
EntityCollection<User> Users
I have tried with following code
public void update(int userId, string newusername, list<int> roleList)
{
using (DBEntites context = new DBEntites())
{
User objUserInDb = new User();
objUserInDb.UserID = userId;
Context.Users.Attach(objUserInDb);
objUserInDb.Name = newusername;
objUserInDb.Roles.Clear();
// TRIED TO USE The remove method objUserInDb.Roles.Remove(entity),
// is returning false
foreach (long pkIDToAdd in roleList)
{
Role objRoleInDb = new Role();
objRoleInDb.RoleId = pkIDToAdd;
// GIVES EXCEPTION
//An object with the same key already exists in the ObjectStateManager.
//The ObjectStateManager cannot track multiple objects with the same
//
context.Roles.Attach(objRoleInDb);
objUserInDb.Roles.Add(objRoleInDb);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
例外是因为某些角色已加载到上下文中。你可以这样做,
The exception is because some roles are loaded to the context already. You can do like this,