Is it possible to specify child objects that should also be deleted with their parents in Linq?

StackOverflow https://stackoverflow.com/questions/22158120

  •  19-10-2022
  •  | 
  •  

문제

In order to make sure that all child elements are deleted, I am currently having to do this:

ComponentType type = db.ComponentTypes.First(t => t.ID == Convert.ToInt32(button.CommandArgument));
db.RigActions.DeleteAllOnSubmit(type.Components.SelectMany(c => c.RigActions));
db.Components.DeleteAllOnSubmit(type.Components);
db.ComponentTypes.DeleteOnSubmit(type);
db.SubmitChanges();

For maintainability purposes, this is scaring me that I (or another developer) might overlook all of the cleanup necessary when a parent (or in this case - a parent of a parent) element is deleted.

Does Linq have any type of dependency property I can set so that when a Component type is deleted, it deletes all component records, and all actions belonging to each component record?

올바른 솔루션이 없습니다

다른 팁

I'm not sure it can be done in Linq (it probably can). It would be far easier if you defined a CASCADE DELETE on your database though. That way, child records would be deleted automatically and you'd not need to worry about forgetting anything.

This article might give you a bit more information

http://www.mssqltips.com/sqlservertip/2743/using-delete-cascade-option-for-foreign-keys/

This article is about setting up cascade delete within EF

Entity Framework on delete cascade

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top