Delete Entity in Many to Many Relation. Don't get error but entity is not deleted
Posted
by
Shapper
on Stack Overflow
See other posts from Stack Overflow
or by Shapper
Published on 2012-12-14T23:26:39Z
Indexed on
2012/12/15
23:04 UTC
Read the original article
Hit count: 152
entity-framework
|delete
I have, in EF5, two entities: User and Role.
Between User and Role there is a many to many relation.
I don't have an entity for the UserRoles database which sets the relation.
I have a User and I want to delete a role without loading it from the database.
Context context = new Context();
User user = context.Users.First(x => x.Id == 4);
user.Roles = new List<Role>();
Role role = new Role { Id = 20 };
context.Roles.Attach(role);
user.Roles.Remove(role);
context.SaveChanges();
I don't get any error but the role is not removed.
Any idea why?
Thank you, Miguel
© Stack Overflow or respective owner