Imagine this database model:
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public string Firstname { get; set; }
public ICollection<Role> Roles { get; set; }
}
public class Role
{
public int Id { get; set; }
public string RoleType { get; set; }
public ICollection<User> Users { get; set; }
}
There is an intermediate table that looks like this (not present as a POCO):
UserRole UserId RoleId
Then i decided to delete a role, which means that all the relationships of this role in the intermediate table should be deleted as well.
No matter what I try, I get either this error message:
The DELETE statement conflicted with the REFERENCE constraint "FK_UserRole_Role". The conflict occurred in database "dbname", table "dbo.UserRole", column 'RoleId'.
Or this error message:
The object cannot be deleted because it was not found in the ObjectStateManager.
The first error message comes from this try:
_dataContext.Entry(role).State = EntityState.Deleted;
_dataContext.SaveChanges();
This one is responsible for the second error message:
_dataContext.Circuit.Remove(role);
_dataContext.SaveChanges();
I did a few other tries, but I don't remember them as I have been trying to get this working from this morning (GMT +2).
Can anyone point me into the right direction?