c# - 同一个表的多个外键具有级联更新和删除

标签 c# entity-framework ef-code-first

我正在尝试为一个实体添加多个外键,该实体将通过级联更新和删除连接到同一其他表。

所以我有系列参数实体:

public class Series : Entity<int>
{
    public string Name { get; set; }
    public int IterationsId { get; set; }
    public int KId { get; set; }
    public int LambdaId { get; set; }
    public int GradientId { get; set; }
    public int ImproveId { get; set; }
    public int TrainingId { get; set; }
    public DateTime CreateDateTime { get; set; }
    public DateTime? ChangeDateTime { get; set; }
    public virtual Argument Iterations { get; set; }
    public virtual Argument K { get; set; }
    public virtual Argument Lambda { get; set; }
    public virtual Argument Gradient { get; set; }
    public virtual Argument Improve { get; set; }
    public virtual Argument Training { get; set; }
}

public class Argument : Entity<int>
{
    public Argument()
    {
        Values = new List<ArgumentValue>();
    }

    public int Min { get; set; }
    public int Max { get; set; }
    public int Step { get; set; }
    public ArgumentType ArgumentType { get; set; }
    public virtual ICollection<ArgumentValue> Values { get; set; }
}

使用他们的映射:

public class SeriesMap : BaseMap<Series, int>
{
    public SeriesMap()
    {
        ToTable("Series");

        Property(x => x.Name).IsRequired();
        Property(x => x.CreateDateTime).IsRequired();
        Property(x => x.ChangeDateTime).IsOptional();

        HasRequired(x => x.Iterations).WithMany().HasForeignKey(x => x.IterationsId).WillCascadeOnDelete(true);
        HasRequired(x => x.K).WithMany().HasForeignKey(x => x.KId).WillCascadeOnDelete(true);
        HasRequired(x => x.Lambda).WithMany().HasForeignKey(x => x.LambdaId).WillCascadeOnDelete(true);
        HasRequired(x => x.Gradient).WithMany().HasForeignKey(x => x.GradientId).WillCascadeOnDelete(true);
        HasRequired(x => x.Improve).WithMany().HasForeignKey(x => x.ImproveId).WillCascadeOnDelete(true);
        HasRequired(x => x.Training).WithMany().HasForeignKey(x => x.TrainingId).WillCascadeOnDelete(true);
    }
}

public class ArgumentMap : BaseMap<Argument, int>
{
    public ArgumentMap()
    {
        ToTable("Argument");

        Property(x => x.Min).IsRequired();
        Property(x => x.Max).IsRequired();
        Property(x => x.Step).IsRequired();

        HasMany(x => x.Values);
    }
}

但是当我尝试创建这样的模型时,我收到一条异常消息:

在表“Series”上引入外键约束“FK_dbo.Series_dbo.Argument_ImproveId”可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。

当我改变

.WillCascadeOnDelete(true);

.WillCascadeOnDelete(false);

模型创建但不是我想要的。 是否可以通过级联更新/删除对同一个表进行多重引用?

最佳答案

基本上,当级联路径从表 A 中的列 col1 到表 A 中的列 col2 时(A => A),SQL Server 不允许对内部关系创建级联操作。

因此,强制执行此关系的级联删除的唯一方法是直接在 SQL 数据库上使用 SQL 触发器或迭代集合并删除所有子对象。

希望对你有帮助!

关于c# - 同一个表的多个外键具有级联更新和删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31771774/

相关文章:

c# - 可以添加到 Azure 队列的 Base64 编码字符串的最大长度是多少?

c# - Entity Framework 代码首先与 MySql 服务器无法运行迁移

c# - Attach() 在 Entity Framework 中究竟做了什么?

entity-framework - 实体代码优先与共享主机生产数据库

c# - 如何使用 ASP.net EF Codefirst 数据注释将 SQL Server 中的列设置为 varchar(max)?

c# - epplus 4.1 中取消合并并清除单元格

c# - 使用 Excel 数据的简单任务未完成运行导致 "ContextSwitchDeadlock"

c# - 如何在不重新编译的情况下处理新版本的第三方 .Net 程序集?

c# - Linq 到实体查询

c# - 使用 EF Code First 建模员工-助理关系