entity-framework - EF Code First 级联删除不起作用

标签 entity-framework

我有 4 张 table :

用户表

public enum SEX { Male, Female }

public abstract class User
{

    public int UserID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public SEX Sex { get; set; }
}

Doctor表继承自User

[Table("Doctor")]
public class Doctor : User
{
    public string Department { get; set; }
    public string Occupation { get; set; }
    public string CabinetNumber { get; set; }

    public virtual List<Treat> Treats { get; set; }
}

Patient 表继承自 User

[Table("Patient")]
public class Patient : User
{
    public int InsuranceNumber { get; set; }
    public int CardNumber { get; set; }

    public virtual List<Treat> Treats { get; set; }
}

public class Treat
{
    public int TreatId { get; set; }

    public int DoctorUserId { get; set; }
    public int PatientUserId { get; set; }

    public virtual Doctor Doctor { get; set; }
    public virtual Patient Patient { get; set; }
}

public class HospitalContext: DbContext
    {
        public HospitalContext() : base("DBConnectionString") {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<HospitalContext>());
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Treat>()
                .HasRequired(x => x.Doctor)
                .WithMany( x => x.Treats)
                .HasForeignKey( x => x.DoctorUserId)
                .WillCascadeOnDelete(true);

            modelBuilder.Entity<Treat>()
                .HasRequired(x => x.Patient)
                .WithMany( x => x.Treats)
                .HasForeignKey( x => x.PatientUserId)
                .WillCascadeOnDelete(true);

            base.OnModelCreating(modelBuilder);
        }
        public DbSet<User> Users { get; set; }
        public DbSet<Treat> Treats { get; set; }
    }

我在这里找到了很多答案,但没有一个有效。我花了几个小时试图让它发挥作用。我知道 Entity Framework 必须在存在一对多关系时启用级联删除,但事实并非如此

最佳答案

Entity Framework 不应用带有TPT(每个类型的表)继承的级联删除。您可以通过 Code Fist 迁移来解决此问题:

CreateTable(
    "dbo.Treats",
    c => new
    {
        TreatId = c.Int(nullable: false, identity: true),
        DoctorUserId = c.Int(nullable: false),
        PatientUserId = c.Int(nullable: false),
    })
    .PrimaryKey(t => t.TreatId)
    .ForeignKey("dbo.Doctor", t => t.DoctorUserId, cascadeDelete: true)
    .ForeignKey("dbo.Patient", t => t.PatientUserId, cascadeDelete: true)
    .Index(t => t.DoctorUserId)
    .Index(t => t.PatientUserId);

重要的部分是cascadeDelete: true。您必须在迁移代码生成后手动添加它。之后您将在数据库中进行级联删除:

FOREIGN KEY ([DoctorUserId]) REFERENCES [dbo].[Doctor] ([UserID]) ON DELETE CASCADE,
FOREIGN KEY ([PatientUserId]) REFERENCES [dbo].[Patient] ([UserID]) ON DELETE CASCADE

关于entity-framework - EF Code First 级联删除不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32541366/

相关文章:

entity-framework - Entity Framework 代码优先 - 定义与 MembershipUser 的关系

entity-framework - Entity Framework 更新种子数据——数据移动迁移

c# - 用于 3 个表连接的 Entity Framework 7 Lambda 表达式

ASP.Net 分层应用程序 - 在层之间共享实体数据模型

entity-framework - 如何优化我的 EF Code First 查询?

postgresql - 为什么每秒对我的Asp.Net Core API的并发请求数增加时,响应时间会增加

c# - 在 .NET Entity Framework 4 中使用 SP 添加自定义导航属性

c# - 用于显式加载引用和集合的 EF Core 辅助方法

.net - .net 中的错误 :A referential integrity constraint violation occurred on db. SaveChanges()?

c# - 显示原始值 Entity Framework 7