如何确保在 EF Code First 中对表关系启用级联删除?

发布于 2024-10-27 07:15:28 字数 186 浏览 1 评论 0原文

我想使用代码优先在表上启用级联删除。从头开始重新创建模型时,即使自动设置关系,也不会设置 CASCADE DELETE。奇怪的是,它确实为某些具有多对多关系的表启用了此功能,但您可能认为这可能会出现问题。

设置: 表A<-表B。

表B的FK指向表A的PK。

为什么这行不通?

I would like to enable CASCADE DELETE on a table using code-first. When the model is re-created from scratch, there is no CASCADE DELETE set even though the relationships are set-up automatically. The strange thing is that it DOES enable this for some tables with a many to many relationship though, which you would think it might have problems with.

Setup:
Table A <- Table B.

Table B's FK points to Table A's PK.

Why would this not work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨的味道风的声音 2024-11-03 07:15:28

您没有获得级联删除的可能原因是您的关系是可选的。示例:

public class Category
{
    public int CategoryId { get; set; }
}

public class Product
{
    public int ProductId { get; set; }
    public Category Category { get; set; }
}

在此模型中,您将获得一个 Product 表,该表具有指向 Category 表的外键,但该键可为空,并且默认情况下数据库中没有级联删除设置。

如果您想拥有所需的关系,那么您有两个选择:

注释:

public class Product
{
    public int ProductId { get; set; }
    [Required]
    public Category Category { get; set; }
}

Fluent API:

modelBuilder.Entity<Product>()
            .HasRequired(p => p.Category)
            .WithMany();

在这两种情况下,都会自动配置级联删除。

如果您希望关系可选但带有级联删除,您需要明确配置:

modelBuilder.Entity<Product>()
            .HasOptional(p => p.Category)
            .WithMany()
            .WillCascadeOnDelete(true);

编辑:在最后一个代码片段中,您也可以简单地编写.WillCascadeOnDelete()。此无参数重载默认为 true 以设置级联删除。

有关详细信息,请参阅文档

Possible reason why you don't get cascading delete is that your relationship is optional. Example:

public class Category
{
    public int CategoryId { get; set; }
}

public class Product
{
    public int ProductId { get; set; }
    public Category Category { get; set; }
}

In this model you would get a Product table which has a foreign key to the Category table but this key is nullable and there is no cascading delete setup in the database by default.

If you want to have the relationship required then you have two options:

Annotations:

public class Product
{
    public int ProductId { get; set; }
    [Required]
    public Category Category { get; set; }
}

Fluent API:

modelBuilder.Entity<Product>()
            .HasRequired(p => p.Category)
            .WithMany();

In both cases cascading delete will be configured automatically.

If you want to have the relationship optional but WITH cascading delete you need to configure this explicitely:

modelBuilder.Entity<Product>()
            .HasOptional(p => p.Category)
            .WithMany()
            .WillCascadeOnDelete(true);

Edit: In the last code snippet you can also simply write .WillCascadeOnDelete(). This parameterless overload defaults to true for setting up cascading delete.

See more on this in the documentation

抱着落日 2024-11-03 07:15:28
modelBuilder
.Entity<Product>()
.HasRequired(p => p.Category)
.WithMany(x => x.Products)
.WillCascadeOnDelete(true);
modelBuilder
.Entity<Product>()
.HasRequired(p => p.Category)
.WithMany(x => x.Products)
.WillCascadeOnDelete(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文