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.

I have used EntityFramework(Code First). Following is the code of "PermissionRecord" class

public class PermissionRecord : BaseEntity
{
    private ICollection<UserRole> _userRoles;    

    public string Name { get; set; }    

    public string SystemName { get; set; }

    public string Category { get; set; }

    public virtual ICollection<UserRole> UserRoles
    {
        get => _userRoles ?? (_userRoles = new List<UserRole>());
        protected set => _userRoles = value;
    }    
}
Ram
  • 41
  • 3
  • Please refer: [LINK](https://stackoverflow.com/questions/6699897/entity-framework-the-relationship-between-the-two-objects-cannot-be-defined-be) and [LINK](https://stackoverflow.com/questions/5693843/entity-framework-multiple-object-contexts/5695009#5695009) – Shawn Xiao May 21 '20 at 06:10
  • 1
    @Shawn Xiao Thank you, I have already referred these links. The problem occurred because of Unity, I just removed it and used Autofac. Its working now. – Ram May 21 '20 at 09:09

0 Answers0