gpt4 book ai didi

c# - Entity Framework : Why WillCascadeOnDelete() Method is ignored?

转载 作者:可可西里 更新时间:2023-11-01 08:12:31 33 4
gpt4 key购买 nike

这是我的情况:

public abstract class Article
{
[key]
public Guid Guid { get; set;}

public string Name { get; set;}
.
.
.
}

public class Download : Article
{
...
}

public abstract class Category : Article
{
...
}

public class DownloadCategory : Category
{
....
}

然后我应该像这样在 Download 和 DownloadCategory 之间建立多对多关系:

public class DownloadInCategory
{
[Key, Column(Order = 1), Required]
[ForeignKey("Download")]
Public Guid DownloadGuid { get; set; }

Public Download Download { get; set; }

[Key, Column(Order = 2), Required]
[ForeignKey("Category")]
Public Guid CategoryGuid { get; set; }

Public DownloadCategory Category { get; set; }
}

当我调用 Add-Migration 时,为 DownloadInCategory 实体创建的迁移是:

CreateTable("dbo.DownloadInCategories",
c => new
{
CategoryGuid = c.Guid(nullable: false),
DownloadGuid = c.Guid(nullable: false),
})
.PrimaryKey(t => new { t.CategoryGuid, t.DownloadGuid })
.ForeignKey("dbo.DownloadCategories", t => t.CategoryGuid)
.ForeignKey("dbo.Downloads", t => t.DownloadGuid, cascadeDelete: true)
.Index(t => t.CategoryGuid)
.Index(t => t.DownloadGuid);

这是我的问题:正如您所注意到的,它没有将 cascadeDelete: true 添加到其中一个外键。为什么!!!!!!?????

我应该提一下,我没有更改任何 modelbuilder 约定。所以这个模式应该在迁移中删除时添加级联。我的属性是 [Required]

我做错了什么?

谢谢大家...

更新:请注意 ArticleCategory 类是抽象的。我在上面更改了类

更新 2:此模式没有逻辑问题。如果我手动编辑迁移,它会正常更新数据库。

更新 3:我的 EF 继承方法是 TPC

更新 4:经过一些调查和测试,问题似乎是从 Category 继承而来的。当DownloadCategory继承自Category时,不部署Cascade。但是当我直接从 Article 继承 DownloadCategory 时,部署了 Cascade。但是为什么又是这样呢?

最佳答案

我认为这是因为:

DownloadCategory : Category : Article

对比

Download : Article

关键在 Article 类上。多个 DownloadCategories 可以使用相同的 Category,因此它不会级联删除,因为这可能会使其他 DownloadCategory 损坏。

这可能是 Entity Framework 的失败,因为您使用的是 TPC,所以应该可以推断出这一点。看看this article解决方法。

特别是这些部分:

In most cases the Entity Framework can infer which type is the dependent and which is the principal in a relationship. However, when both ends of the relationship are required or both sides are optional the Entity Framework cannot identify the dependent and principal. When both ends of the relationship are required, use WithRequiredPrincipal or WithRequiredDependent after the HasRequired method. When both ends of the relationship are optional, use WithOptionalPrincipal or WithOptionalDependent after the HasOptional method.

// Configure the primary key for the OfficeAssignment 
modelBuilder.Entity<OfficeAssignment>()
.HasKey(t => t.InstructorID);

modelBuilder.Entity<Instructor>()
.HasRequired(t => t.OfficeAssignment)
.WithRequiredPrincipal(t => t.Instructor);

You can configure cascade delete on a relationship by using the WillCascadeOnDelete method. If a foreign key on the dependent entity is not nullable, then Code First sets cascade delete on the relationship. If a foreign key on the dependent entity is nullable, Code First does not set cascade delete on the relationship, and when the principal is deleted the foreign key will be set to null.

You can remove these cascade delete conventions by using:

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>()
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>()

The following code configures the relationship to be required and then disables cascade delete.

modelBuilder.Entity<Course>() 
.HasRequired(t => t.Department)
.WithMany(t => t.Courses)
.HasForeignKey(d => d.DepartmentID)
.WillCascadeOnDelete(false);

关于c# - Entity Framework : Why WillCascadeOnDelete() Method is ignored?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30729564/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com