chridam
chridam

Reputation: 103375

Removing a row from table with two foreign keys entity framework

I have a table which consists of only 2 foreign keys as columns. These keys in the table represent a many to many association between 2 other tables. For example: The table is RoleGroup, and the only 2 columns are GroupId and RoleId, both foreign keys to Group and Role tables, respectively. The generated EF object from database didn't create the RoleGroup object, only navigation properties with the other tables. I can insert data in RoleGroup table in EF as follows:

Group grp = context.Groups.Where(g => g.Id == 8);
Role role = context.Roles.Where(r => r.Id == '001c');
grp.Roles.Add(role);
context.SaveChanges();

Now, how do I remove a row within the table RoleGroup in EF e.g. remove a group (with Id = 5) from a role with Id say '001b'?

Upvotes: 1

Views: 2265

Answers (1)

Chris Midolo
Chris Midolo

Reputation: 191

Entity framework, like most ORM's abstracts away link tables. Presuming that you've got everything set up correctly you can remove the record by removing the link.

Role role = //Whatever Role;
group.Roles.Remove(role);

Upvotes: 1

Related Questions