Sql server cascade update delete




















It means that the child data is set to NULL when the parent data is deleted or updated. It means that the child data is set to their default values when the parent data is deleted or updated.

Share on:. Click on Close and save the table in the designer. Click Yes in the warning message window. Once you click on Yes, a foreign key with delete rule is created. Now update CountryID in the Countries for a row which also updates the referencing rows in the child table States. In either case the result is the same: the blog is not deleted, but the posts that are no longer associated with any blog are deleted:.

Cascade delete and deleting orphans are closely related. EF Core always applies configured cascading behaviors to tracked entities. The exact timing of when cascading behaviors happen to tracked entities can be controlled using ChangeTracker. CascadeDeleteTiming and ChangeTracker.

See Changing Foreign Keys and Navigations for more information. Many database systems also offer cascading behaviors that are triggered when an entity is deleted in the database. For example, using the model above, the following table is created for posts when using SQL Server:. If we know that the database is configured like this, then we can delete a blog without first loading posts and the database will take care of deleting all the posts that were related to that blog.

For example:. Notice that there is no Include for posts, so they are not loaded. SaveChanges in this case will delete just the blog, since that's the only entity being tracked:. This would result in an exception if the foreign key constraint in the database is not configured for cascade deletes. Databases don't typically have any way to automatically delete orphans.

This is because while EF Core represents relationships using navigations as well of foreign keys, databases have only foreign keys and no navigations. This means that it is usually not possible to sever a relationship without loading both sides into the DbContext. Do not configure cascade delete in the database when soft-deleting entities. This may cause entities to be accidentally really deleted instead of soft-deleted. Some databases, most notably SQL Server, have limitations on the cascade behaviors that form cycles.

For example, consider the following model:. This model has three relationships, all required and therefore configured to cascade delete by convention:. This is all reasonable if a bit draconian in blog management policies! Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.

Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.



0コメント

  • 1000 / 1000