Recently, the EF write code (the EF of the database update) is used, and then the individual data entities are connected by a self-built relationship. Set END1 on DELETE as cascade in the relationship, it is a pity that it is impossible to cascade Delete, the internet looked at a large number of data found that the problem is stored in the EF data is not cascading deleted entities data, it is no wonder that the EF does not use the data he is not loaded into memory. So quoting a foreigner's article for reference
Just let EF load the memory. And then we have the following methods.
1. First set END1 on DELETE as Cascade in the EF Entity Framework.
2. Write the deletion procedure, the part that wants to cascade deletes is loaded as follows
var db = new Investp2pentities ();
var rm = Db.Company.First (p => p.id = = 1);
Set the END1 on DELETE cascade and then load the part of the deletion to the
var temp=rm.govinorg_gov;//this is the navigation property
temp=rm.govinorg_org;// This is the navigation properties
//Loading complete
db.Company.Remove (RM);
Db. SaveChanges ();
Imagine to your database you have a cascade delete on a FK relationship.
Something like this:
Cascadedeleteindatabase
Here's the Delete rule says this when a Category are deleted all the related products should to deleted.
If you are generate an EF model out of this database for the surface looks no different from normal:
ProductCategory
But If you dig to the CSDL section of the XML you'll be here:
Notice the element, this tells the EF, then a Category is deleted the related products would be too.
I deliberately said would and rather than should, because the EF does not take responsibility to cascading the delete in T He database.
The EF is responsible to the correctness of the ObjectContext after SaveChanges (). So the EF attempts to synchronize of the ObjectContext, with the expected database state after the expected cascade in the DA Tabase.
A tell tale sign of ' this ' is ' that if ' you ' open up something like SQLProfiler and you'll notice the EF issuing DELETE requests For dependent entities that it knows about (i.e. then are loaded in the ObjectContext) when a principal is deleted.
Essentially what is happening this Entity Framework expects that deleting the principal in the database, would Delete all it's dependents in the database. So it issues, what should is, a redundant DELETE to request itself so the dependents already loaded are deleted from the O Bjectcontext.
The key thing to the "The EF does not retrieve all dependent entities and issue deletes for Them:it TES dependents that are already in memory.
So here are the golden rules:
If you add an Cascade delete the rule to the model, you must have a corresponding delete rule in the database.
If you are absolutely insist on breaking rule (1) for some reason, Cascade'll only work if you have all the dependents Loade D in memory.
(2) is not recommended!!!
While we did our best to keep the ObjectContext and database in sync, our attempts can fail if you have multiple levels of Cascade Delete.
For example, if your have this:
Category–> product–> Order
And deleting a Category deletes its products which the turn its Orders.
The EF can, in rare circumstances, fail to sync up and the database when you delete a Category.
For example if your have an order loaded it related to a Category via a unloaded Product, and you delete the Category , the EF won ' t know to delete the order.
This is means the order would remain in the ObjectContext in the unchanged state, despite it has been deleted in the Databa Se.
Forewarned is forearmed.