0

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?

Mister Epic
  • 16,295
  • 13
  • 76
  • 147
drewwyatt
  • 5,989
  • 15
  • 60
  • 106
  • Linq is a querying language, it has no intrinsic data management features. This is more of an Entity Framework question, but if your referential integrity is setup right in your database, you should never be able to delete a parent without deleting its children. – Mister Epic Mar 03 '14 at 21:48
  • lINQ is merely the query language you are using. Are you using LINQ to SQL(.dbml) LINQ TO Entities(.edmx), or something else? – Olaf Mar 03 '14 at 21:50

1 Answers1

0

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

Community
  • 1
  • 1
Andrew
  • 2,315
  • 3
  • 27
  • 42