Search code examples
c#asp.netasp.net-mvcentity-frameworkmany-to-many

Entity Framework - how to retrieve only one record from bridge table and delete it?


I have two tables connected as many-to-many relationship in EF. Let's assume, that I have table "Person" & "Groups". What I want to achieve is to delete only one group from specified Person. I dont want to delete either Person nor Group, just the record in Bridge Table. How to do that? Is there a need to include "auto-generated" bridge table in DBContext?

Thanks.


Solution

  • Using EF 6 Code first configure your entities to be many to many as described in this article (see here for the classes and relationships). If you're working with a Person entity, then you would remove the group in question from the entities Groups collection, then save the entity. If you're working with a Group, you would remove the Person from the entities People collection and save.