Guest User

Guest User

  • Tech Writer
  • 36
  • 1.2k

Entity framework c# error on updating a record

May 21 2020 1:09 AM
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)