I have this object:
public class Profile
{
public int Id {get;set;}
public List<User> Users {get;set;}
}
and I have a page to delete profiles.. so I want to delete a profile if there is not users related..
I tried this:
var profile = _db.Profile
.Include(p => p.Users)
.SingleOrDefault(p => p.Id == id);
_db.Profile.Remove(profile);
_db.SaveChanges();
but this automatically delete all users related(I don’t undestand why since I don’t set any especial configuration for that).
How can I avoid this?
How can I get a exception when I try to SaveChanges?
I would love to avoid checking the Users.Count directly in my code to do it.
you can use the extension method
for a specific relation,
or disable all delete cascade with the
OneToManyCascadeDeleteConventionThis will show you some more details (EF 4 CTP5, but should be valuable)