Can I use Data Annotations to perform a Cascade Delete with Entity Framework 4.1 RC?
Solution 1
Putting required on the Product table Category relationship field solves this
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
[Required] //<======= Forces Cascade delete
public Category Category { get; set; }
}
Solution 2
I like to turn off cascade delete by default (by removing the OneToManyCascadeDeleteConvention
)
I was then hoping to add them back in via annotations, but was surprised that EF doesn't include a CascadeDeleteAttribute
.
After spending way too long working around EF's ridiculous internal accessor levels, the code in this gist adds a convention that allows attributes to be used: https://gist.github.com/tystol/20b07bd4e0043d43faff
To use, just stick the [CascadeDelete]
on either end of the navigation properties for the relationship, and add the convention in your DbContext's OnModeCreating callback. eg:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Add<CascadeDeleteAttributeConvention>();
}
And in your model:
public class BlogPost
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
[CascadeDelete]
public List<BlogPostComment> Comments { get; set; }
}
Solution 3
Not sure on Data Annotations, but you can add it in the database by modifying the actual relationship.
Looks like the answer is no for data annotations: http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/394821ae-ab28-4b3f-b554-184a6d1ba72d/
This question appears to show how to do it with the fluent syntax, but not sure if that applies for 4.1 RC EF 4.1 RC: Weird Cascade Delete
Solution 4
As an additional example to Tyson's answer, I use the [CascadeDelete]
attribute like follows in an entity, which successfully adds the "Cascade" delete rule to the Parent
-Child
relation.
public class Child
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
[SkipTracking]
public Guid Id { get; set; }
[CascadeDelete]
public virtual Parent Parent { get; set; }
[Required]
[ForeignKey("Parent")]
public Guid ParentId { get; set; }
}
Related videos on Youtube
Bender
Updated on December 01, 2020Comments
-
Bender over 3 years
When using data annotations with EF4.1 RC is there an annotation to cause cascade deletes?
public class Category { public int Id { get; set; } [Required] public string Name { get; set; } public ICollection<Product> Products { get; set; } } public class Product { public int Id { get; set; } public string Name { get; set; } public Category Category { get; set; } }
Using this model the constraint generated is:
ALTER TABLE [Product] ADD CONSTRAINT [Product_Category] FOREIGN KEY ([Category_Id]) REFERENCES [Categorys]([Id]) ON DELETE NO ACTION ON UPDATE NO ACTION;
If not how is it achieved?
-
Rafael over 8 yearsThis is better than the chosen answer because the Requiered attribute imples a not-nullable foreign key. And it works with EF 6. Thanks!
-
Alireza almost 8 yearsGreat solution, also, please note that the Convention class should be added to the
modelBuilder.Conventions
inOnModelCreating
method. -
Elias MP over 7 yearsVery creative. Thanks.
-
Shimmy Weitzhandler almost 7 yearsWhat gets deleted upon deletion of what?
-
Derrick about 6 yearsThis only seems to work on [Required] relationships, so in my case I modified CascadeDeleteAttribute to inherit from RequiredAttribute for clarity
-
KlevisGjN about 3 yearsSearching for
ef core foreign key annotation cascade delete
found nothing. Your comment worked as expected. Thanks