Can I use Data Annotations to perform a Cascade Delete with Entity Framework 4.1 RC?

后端 未结 4 554
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 00:46

When using data annotations with EF4.1 RC is there an annotation to cause cascade deletes?

public class Category
{
    public int Id { get; set; }
    [Requi         


        
相关标签:
4条回答
  • 2020-12-08 00:57

    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; }
    }
    
    0 讨论(0)
  • 2020-12-08 01:09

    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; }
    }
    
    0 讨论(0)
  • 2020-12-08 01:10

    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; } 
    }
    
    0 讨论(0)
  • 2020-12-08 01:21

    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

    0 讨论(0)
提交回复
热议问题