.net 我可以使用数据注释通过 Entity Framework 4.1 RC 执行级联删除吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5569072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:26:56  来源:igfitidea点击:

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

.netentity-frameworkentity-framework-4.1

提问by Bender

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

在 EF4.1 RC 中使用数据注释时,是否有导致级联删除的注释?

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?

如果不是,它是如何实现的?

回答by Bender

Putting required on the Product table Category relationship field solves this

在 Product 表 Category 关系字段上放置 required 解决了这个问题

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }

    [Required]  //<======= Forces Cascade delete
    public Category Category { get; set; }
}

回答by Tyson

I like to turn off cascade delete by default (by removing the OneToManyCascadeDeleteConvention)

我喜欢默认关闭级联删除(通过删除OneToManyCascadeDeleteConvention

I was then hoping to add them back in via annotations, but was surprised that EF doesn't include a CascadeDeleteAttribute.

然后我希望通过注释将它们添加回来,但很惊讶 EF 不包含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

在解决 EF 荒谬的内部访问器级别上花费了太长时间后,此要点中的代码添加了一个允许使用属性的约定: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:

要使用,只需[CascadeDelete]在关系的导航属性的两端粘贴,并在 DbContext 的 OnModeCreating 回调中添加约定。例如:

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; } 
}

回答by taylonr

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/

看起来数据注释的答案是否定的: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

这个问题似乎展示了如何使用流畅的语法来做到这一点,但不确定这是否适用于 4.1 RC EF 4.1 RC: Weird Cascade Delete

回答by j00hi

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-Childrelation.

作为Tyson's answer的附加示例,我[CascadeDelete]在实体中使用如下属性,该属性成功地将“级联”删除规则添加到Parent-Child关系。

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; }
}