【问题标题】:Multiple foreign keys to same table with cascade update and delete具有级联更新和删除的同一张表的多个外键
【发布时间】:2015-08-02 12:10:12
【问题描述】:

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

所以我有 SeriesArgument 实体:

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”上引入 FOREIGN KEY 约束“FK_dbo.Series_dbo.Argument_ImproveId”可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。

当我改变时

.WillCascadeOnDelete(true);

.WillCascadeOnDelete(false);

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

【问题讨论】:

  • 每次删除其中一个参数类型时是否要删除一个系列?这就是您收到错误的原因。

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


【解决方案1】:

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

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

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    相关资源
    最近更新 更多