To say I had proxy class
and Lazyloading
both disabled. and I two objects with a many to many
relation. When coming to delete a record, I had the codes:
public void DeleteUser(List<int> ids)
{
using (var dbContext = new AccountDbContext())
{
dbContext.Users.Include("Roles").Where(u => ids.Contains(u.ID)).ToList().ForEach(a => {
//a.Roles.Clear();
//dbContext.Users.Remove(a);
dbContext.Delete(a);
});
dbContext.SaveChanges();
}
}
It looks like dbContext.Delete(user)
will delete the Roles
related to the user
[which in the database a many to many record is deleted ]. As I thought cascade delete
is enabled. So can I say user.Roles.Clear();
is totally unnecessary here? And when should I implement a Clear
method?