Default cascading delete in EF Code First

Ralph Jansen Blog

In the new Entity Framework 4.1 Code First you can easily create a database on your POCO objects. But the down side (sometimes) is that cascading delete is turned on by default.

Say you have two objects called Department and Manager.

image

image

A manager needs a department and a department can have multiple managers. This is done because we want to have a one-many relationship in this demo. To do this we can make navigation properties. A navigation property is created by the virtual keyword. If you want to create a many relationship, you should use the ICollection<> type.

image

image

If you generate the database now, the tables are generated and a foreign key relationship is made. To do this, EF has generated another column in your Manager table. The column name should be Department_Id. If you open the foreign key relationship you see that the cascading delete relationship is disabled.

But our POCO objects are not really complete. Because how would we know what the key of the Deparment is when I only want to query the Manager object? You can’t because you need the other object. To solve this, you can add a column that is similar as your generated Foreign key column. So, we change our object to this code:

image

If you generate your database again, you see that some things are changed. The automatically generated column Department_Id is no longer there and the DepartmentId column is now used for your foreign key relationship. Only, this is not the only difference in your project. Because if you look at your foreign key relationship, you see that the cascading delete is now enabled.

But why do we want a cascading delete in this example? Because say in example, that if a department is merged with another department and the department record would be deleted from the database, your managers would be deleted as well. Why will they be deleted? Are the fired?!?!?! Can’t they go to another department?

Luckily you can change this default behavior. You can do this with the next line of code in your OnModelCreating override:

image

If you generate your database again with the latest classes (class with DepartmentId) your Foreign Key relationship would not have a cascading delete enabled.

4 thoughts to “Default cascading delete in EF Code First”

  1. I just had this happen. You can also turn it off at the table level:

    modelBuilder.Entity()
    .HasRequired(many => many.relationship)
    .WithMany()
    .WillCascadeOnDelete(false);

  2. I have a question, if you have say three tables, Client, Product and Collection and Product has two foreign keys from Client and Collection,furthermore Collection has a foreign key from Client, for some reason one of the key is forced to be an optional one while the other is allowed to be required by code first in the Product Table, even though if i want both keys to be required, is this due to cascade delete ?

    Thanks

    1. Hey Sanaan,

      I don’t think it is possible to that if understand you right. This comes that when you delete an entity, you will get a circulair delete of multiple entities. That would delete you whole database. But your description is a little bit limmited to give a good answer. Try to make a good topic on stackoverflow and give me the link so I could look at it.

      Ralph

Leave a Reply