简体   繁体   English

代码优先级联删除

[英]Code First Cascade Delete

Morning, I used Code First to build my database, and inheritance hierarchy of Table per Type (TPT) approach was implemented. 早上,我使用Code First构建数据库,并实现了“每种类型的表(TPT)”方法的继承层次结构。 As below is a sample model of my project: 下面是我的项目的示例模型:

public enum Type
{
    A = 0,
    B = 1
}

public abstract class Device
{
    public int DeviceId { get; set; }
    public Type Type { get; set; }
}

[Table("DeviceA")]
public class DeviceA : Device
{
    public int Value { get; set; }
}

[Table("DeviceB")]
public class DeviceB : Device
{
    public int Value { get; set; }
}

And I have a table, which is related to both A & B. The model is as shown below: 我有一张与A和B相关的表。该模型如下所示:

public class Sir
{
    public int SirId { get; set; }

    [Required]
    public virtual DeviceA DeviceA { get; set; }

    public virtual DeviceB DeviceB { get; set; }
}

And the OnModelCreating function as below: 以及OnModelCreating函数如下:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Entity<Sir>()
            .HasRequired(s => s.DeviceA)
            .WithMany()
            .WillCascadeOnDelete(true);

        modelBuilder.Entity<Sir>()
            .HasOptional(s => s.DeviceB)
            .WithMany()
            .WillCascadeOnDelete(false);

        base.OnModelCreating(modelBuilder);
    }

Here comes the problem, when I attempt to delete a device in the superclass. 当我尝试删除超类中的设备时,问题就来了。 A SQL error prompted: 出现SQL错误提示:

The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.DeviceA_dbo.Devices_DeviceId". DELETE语句与REFERENCE约束“ FK_dbo.DeviceA_dbo.Devices_DeviceId”冲突。 The conflict occurred in database "CodeFirst", table "dbo.DeviceA", column 'DeviceId'. 数据库“ CodeFirst”的表“ dbo.DeviceA”的列“ DeviceId”中发生了冲突。

As far as I understand, according to MSDN documentation in http://msdn.microsoft.com/en-us/data/jj591620.aspx (Under the header section of Enabling Cascade Delete ), it states that . 据我了解,根据http://msdn.microsoft.com/zh-cn/data/jj591620.aspx中的 MSDN文档(在“ 启用级联删除 ”的标题部分下),它指出 If a foreign key on the dependent entity is not nullable, then Code First sets cascade delete on the relationship. 如果从属实体上的外键不可为空,则Code First将在关系上设置级联删除。 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. 如果从属实体上的外键可为空,则Code First不会在关系上设置级联删除,并且当删除主体时,外键将设置为null。

But still, I fail to cascade delete a record A in the superclass after trying for hours. 但是,尝试了几个小时后,我仍然无法在超类中级联删除记录A。 Please advise. 请指教。 Thanks ! 谢谢 !

Try mapping the foreign keys: 尝试映射外键:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

    modelBuilder.Entity<Sir>()
        .HasRequired(s => s.DeviceA)
        .WithMany()
        .Map(m => m.MapKey("DeviceA_Id"))
        .WillCascadeOnDelete(true);

    modelBuilder.Entity<Sir>()
        .HasOptional(s => s.DeviceB)
        .WithMany()
        .Map(m => m.MapKey("DeviceB_Id"))
        .WillCascadeOnDelete(false);

    base.OnModelCreating(modelBuilder);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM