EF cascading deletion, ef-level

Source: Internet
Author: User

EF cascading deletion, ef-level
Introduction

 

 

If a Key is specified in the primary table and Required is specified in the subtable, a foreign Key for cascading deletion is not generated in the database. So how can we make EF generate a cascade deletion foreign key in the data?

Interface for configuring the intermediate join deletion function of SQLServer database:

Only the [required] feature is used.

 

A seemingly correct solution. (Solution)

Version: EF6.0.1 RC

   

In one-to-multiple scenarios, when cascading deletion is enabled in the sub-object ing, deleting a parent object will automatically delete all sub-objects under it. Note the following:

√ Make sure that all sub-objects of the parent object are loaded in DbContext.

X If no sub-object is loaded in DbContext, the sub-object will not be deleted cascade,

X For example, if DbContext only loads some sub-objects and deletes them in cascade mode.

Therefore, you can only use Include ("sub-object property name") to query the parent object (see sample code 3) you can also query all sub-objects in DbContext (see sample code 4) and delete the parent object to cascade the sub-objects.

However, note that the above situation only applies when there are few associated sub-items. The Demo can be demonstrated and tested with a small amount of data. This solution should not appear during work.

 

Real solution (permanent solution)

 

The following is an excerpt from the official website of EntityFrameWork.

----------------------------------------------------------------

You can configure cascade delete on a relationship by using the WillCascadeOnDelete method. if a foreign key on the dependent entity is not nullable, then Code First sets cascade delete on the relationship. 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.

You can remove these cascade delete conventions by using:

ModelBuilder. Conventions. Remove <OneToManyCascadeDeleteConvention> ()
ModelBuilder. Conventions. Remove <ManyToManyCascadeDeleteConvention> ()

The following code configures the relationship to be required and then disables cascade delete.

modelBuilder.Entity<Course>()
    .HasRequired(t => t.Department)
    .WithMany(t => t.Courses)
    .HasForeignKey(d => d.DepartmentID)
    .WillCascadeOnDelete(false);

 

How does EF fix cascading deletion?

 

SettingCascadeOn a relation in EF designer instructs EF to executeDELETEStatement for each loaded realated entity. It doesn't say anything aboutON CASCADE DELETEIn the database.

Setting Cascade deletion when using EF needs two steps:

  • SetCascadeOn relation in EF designer. This instruct context thatAll loaded related entitiesMust be deleted prior to deletion of the parent entity. if this doesn't happen EF will throw exception because internal state will detect that loaded childs are not related to any existing parent entity even the relation is required. i'm not sure if this happens before execution of delete statement of the parent entity or after but there is no difference. EF doesn't reload related entities after executing modifications so it simply doesn't know about cascade deletes triggered in the database.
  • SetON CASCADE DELETEOn relation in database. This will instruct SQL to delete all related records whichWere not loaded to contextIn the time of deleting the parent.

The implementation of cascade deletes in EF is strange and quite inefficient but this is how it behaves and if you want to use it, you must modify your application to behaves correctly in this scenario.

 

 

Summary

 

Reference

Configuration and cascading deletion of one-to-one, one-to-many, and many-to-many relationships in EF

Precautions for cascading deletion of Entity framework

Default ing in EF and how to configure database ing using Data Annotations and Fluent APIs

Enabling Cascade Delete

Entity Framework 4.3 delete cascade with code first (Poco)

Cascading Deletes in LINQ to SQL

Cascading deletes with Entity Framework-Related entities deleted by EF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.