Ef6 delete many to relationship table

Remove() with many-to-many relationship doesn't work in Entity Framework

ef6 delete many to relationship table

Creating a many-to-many relationship in EF Core EF6.x created this table for you when you defined a many-to-many relationship, .. Alter the linking table collection, either by adding or removing entries from the collection. The approach that you adopt to deleting entities via the DbSet relationship will have a cascading referential constraint set to Delete or SetNull. Hi All, I am trying to remove an object from a collection in entity framework and passing parameters just to remove the entry from the link table.

ef6 delete many to relationship table

The official Microsoft documentation recommends manually deleting each foreign key for dependent entities: If we look at the documentation for Entity Framework 6 it says that: If a foreign key on the dependent entity is nullable, Code First does not set cascade delete on the relationship, and when the principal is deleted the foreign key will be set to null.

Entity Framework should now set the foreign keys to null for these entities when I delete a student. However as the code stands, I will still get the same referential integrity error.

ef6 delete many to relationship table

The crucial detail here is that the dependent entities must be loaded before the principal entity is deleted. Here I explicitly load both the Equipmentand Transaction entities before removing the student entity: When the Equipment and Transaction entities are loaded into the working memory, EF knows to set the relevant dependent entities foreign keys to null.

This is a good example of letting the conventions work for you! The default Entity Framework is to set null the foreign key on a dependent entity when the principal entity is deleted.

If I wanted to override the default delete behavior setting, for example to set the dependent entities to cascade on delete, then the Fluent API would be the place to configure that. When you want to create a one-to-one relationship without navigational links going both ways. When you have two navigational properties going to the same class. EF Core will find the relationships using its by convention rules because I have used names that it understands: Because I used the classes Book and Author, which EF Core knows are part of the database, then it knows its a relationship.

HasKey method towards the bottom of the code where I define the composite key consisting of the BookId and the AuthorId.

ef6 delete many to relationship table

But if you want to explicitly define the relationships you can, using the. There are loads of configuration rules, which I cover in chapter 6 and 7 of my bookor you can look at the EF Core docs. You can set the keys, but not the relationships, via attributes. Summary — how to add a many-to-many relationship To add a many-to-many relationship in EF Core you need to: Create a linking entity class that has the foreign keys s of the two entities you want to form a many-to-many link between.

In my example these are BookId and AuthorId. It is efficient but not necessary to make these two foreign keys into the composite primary key. This will ensure that there is only ever one link between the two entities you want.

  • Update Many-to-Many Entity using DBContext
  • Configuring Entity Relationships with Entity Framework
  • The context

It is also more efficient in terms of size of the database table. You most likely want a collection relationship to the linking table in one or both of the linked entities. The code below creates a new book with a new author.

Add book ; context.

Solving the Detached Many-to-Many Problem with the Entity Framework

SaveChanges ; Updating a many-to-many relationship It turns out there are two scenarios under which you want to update a relationship. You find this sort of update that happens in a console application, or inside your business logic see this link on business logic and EF Core Disconnected state: This is where the update is split into two halves: This happens on web sites, where in the first stage the user picks what they want done and posts it back. The second stage then has to re-read the data and update it.

I am going to describe these two approaches separately, so you can go to the one that fits the application you are building. Connected state update In the connected state we read in the Book entity and update it immediately, i. I am using a Sqlite, in-memory database, which I seed with four books with known titles and authors.

Deleting data via the DbSet | Learn Entity Framework Core

Here I load the Book entity using an Include method to load the AuthorLinks at the same time, as tracked entities. The default way of loading data in EF Core is as tracked entities, that is, EF Core takes a copy of the loaded data so that it can detect if any changes have happened since they were loaded. You can turn off tracking by including the method.

AsNoTracking to your query useful in read-only queries, as it makes them a little faster. Summary — Connected state many-to-many update To update a many-to-many relationship in the connected state: In my example I loaded the Book entity I wanted to change along with its AuthorLinks property, which holds the collection of the linking entities in the BookAuthor table.

Alter the linking table collection, either by adding or removing entries from the collection. Disconnected state The disconnected state happens when the initial read of the data and update of the data are done separately, i. This happens in a web application which has two stages: The first stage is where the user is presented with the book, its current author s and a list of possible authors to choose from. Once they have chosen the author to add then press a button which takes their choice back to the web application In the second stage the web application needs to re-read the data and execute the database update.

This unit test uses two, separate instances of the DbContext to simulate the disconnected state.