2
Answers

Entity framework c# error on updating a record

Guest User

Guest User

4y
1.4k
1
I am getting the error "The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects." when updating the Permission record with UserRoles navigation.
 
var permission = _permissionService.GetPermissionRecordBySystemName(permissionSystemName);
if (permission != null)
{
var userRole = _userService.GetUserRoleById(roleId);
if (permission.UserRoles.Contains(userRole))
{
permission.UserRoles.Remove(userRole);
}
else
{
permission.UserRoles.Add(userRole);
}
_permissionService.Update(permission); // Error at this line
}
 
I have used UnityContainer and repository pattern.
Answers (2)