EF 遭遇級聯刪除失敗

最近利用 ef寫代碼,(數據庫更新的ef),然後通過自建關係來連接各個數據實體。在關係裏設置END1 ON DELETE 爲cascade ,非常可惜居然無法進行級聯刪除,在網上查閱了大量資料發現問題竟然是存儲的ef數據裏沒有級聯刪除的實體的數據,這也難怪,ef裏沒有使用到的數據他是不會加載到內存的。所以引用老外的一篇文章以作參考

實在沒轍 只有讓ef加載一下內存纔可以。然後就有了以下的方法
1.首先在ef實體框架裏 設置好END1 ON DELETE 爲cascade。
2.寫刪除程序的時候,把要級聯刪除的那部分加載一下如下

var db = new InvestP2PEntities();

var rm = db.Company.First(p => p.Id == 1);
//設置了end1 on delete cascade 然後把要級聯刪除的那部分加載一下
var temp=rm.govinorg_gov;//這個是導航屬性
temp=rm.govinorg_org;//這個是導航屬性
//加載完畢
db.Company.Remove(rm);

db.SaveChanges();

Imagine that in your database you have a cascade delete on an FK relationship.

Something like this:

CascadeDeleteInDatabase

Here the Delete Rule says that when a Category is deleted all the related Products should be deleted too.

If you generate an EF model from this database you get a model that on the surface looks no different from normal:

ProductCategory

But if you dig into the CSDL section of the XML you will see this:














Notice the element, this tells the EF that when a Category is deleted the related Products will be too.

I deliberately said will and rather than should, because the EF does not take responsibility for cascading the delete in the database.

The EF is responsible for the correctness of the ObjectContext after SaveChanges(). So the EF attempts to synchronize the ObjectContext, with the expected database state after the expected cascade in the database.

A tell tale sign of this is that if you open up something like SqlProfiler, you will notice the EF issuing DELETE requests for dependent entities that it knows about (i.e. that are loaded in the ObjectContext) when a principal is deleted.

Essentially what is happening here is that the Entity Framework expects that deleting the principal in the database, will delete all it’s dependents in the database. So it issues, what should be, a redundant DELETE to request itself so the dependents already loaded are deleted from the ObjectContext.

The key thing to note is that the EF does not retrieve all the dependent entities and issue deletes for them: It only deletes dependents that are already in memory.

So here are the golden rules:

If you add an Cascade delete rule to the model, you MUST have a corresponding DELETE rule in the database.
If you absolutely insist on breaking rule (1) for some reason, Cascade will only work if you have all the dependents loaded in memory.
(2) is not recommended!!!
While we do 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 you have this:

Category –> Product –> Order

And deleting a Category deletes its Products which in turn deletes its Orders.

The EF can, in rare circumstances, fail to sync up with the database when you delete a Category.

For example if you have an Order loaded that is related to a Category via an unloaded Product, and you delete the Category,the EF won’t know to delete the Order.

This means the Order will remain in the ObjectContext in the unchanged state, despite it having been deleted in the database.

Forewarned is forearmed.

發佈了22 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章