How do I set up my domain and the LINQ statement so I can delete a record from a database?
public class Category { public int CategoryId { get; set; } public string Name { get; set; } public List<Product> Products{ get; set; } } public class Product { public int ProductId { get; set; } public string Name { get; set; } public int CategoryId {get; set; } public Category Category{ get; set; } }
What I'd like to do is delete Category and be able to cascade the delete to all the child products.
Is this the only way to do this?
Category category = new Category() { CategoryId = 1 } ; context.AttachTo("Category", category); context.DeleteObject(category); context.Savechanges();
Cascade delete automatically deletes dependent records or sets null to ForeignKey columns when the parent record is deleted in the database. Cascade delete is enabled by default in Entity Framework for all types of relationships such as one-to-one, one-to-many and many-to-many.
Use the ON DELETE CASCADE option to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behavior of the database server prevents you from deleting data in a table if other tables reference it.
A foreign key with cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted. This is called a cascade delete in SQL Server.
On delete cascade is a particularly bad thing to use to clean data because it doesn't discriminate against the data you want the FK to stop the delete for and the data you are trying to completely purge.
You mentioned EF code first which means EF 4.1 but you have shown example of deleting object in EF 4. Correct approach to delete object in EF 4.1 without loading it from database is:
var category = new Category() { CategoryId = 1 }; context.Categories.Attach(category); context.Categories.Remove(category); context.SaveChanges();
If you didn't change anything in configuration of defalut conventions it will also delete all related products because OneToManyCascadeDeleteConventions
ensures that all one-to-many relations are created with ON CASCADE DELETE
. There will be no additional roundtrips to database - only single DELETE
statement for Category
with Id
= 1.
The different situation can occure if you want to delete fully loaded Category
(with loaded Products
navigation property) in such case EF will create separate delete statement for each Product
so you will have N+1 roundtrips to database where N is number of products in category. Here is how cascade delete works in EF. It is related to entity designer but described principles are the same.
Taken from the VS2010 auto generated code:
Category category = db.Categories.Find(id); db.Categories.Remove(category); db.SaveChanges();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With