简体   繁体   English

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

[英]Multiple foreign keys to same table with cascade update and delete

I'm trying to add multiple foreign keys for one entity that will be connected to same other table with cascade update and delete.我正在尝试为一个实体添加多个外键,该实体将通过级联更新和删除连接到同一个其他表。

So I have Series and Argument entity:所以我有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; }
}

With theirs mappings:使用他们的映射:

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);
    }
}

But when I try to create such a model I have a exception with message:但是当我尝试创建这样一个模型时,我有一个消息异常:

Introducing FOREIGN KEY constraint 'FK_dbo.Series_dbo.Argument_ImproveId' on table 'Series' may cause cycles or multiple cascade paths.在表 'Series' 上引入 FOREIGN KEY 约束 'FK_dbo.Series_dbo.Argument_ImproveId' 可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。

When I change当我改变

.WillCascadeOnDelete(true);

to

.WillCascadeOnDelete(false);

model creates but not as I want to.模型创建但不是我想要的。 Is it somehow possible to have muplite references to same table with cascade update/delete?是否可以通过级联更新/删除对同一个表进行多重引用?

Basically, SQL Server does not allow creating cascade actions on Inner relationships – when the cascade path goes from column col1 in table A to column col2 also in table A (A => A).基本上,SQL Server 不允许在内部关系上创建级联操作——当级联路径从表 A 中的列 col1 到表 A 中的列 col2 时(A => A)。

Therefore the only way to enforce cascade delete for this relationships is to use SQL-Triggers directly on the SQL-Database or iterate the collection and remove all children objects.因此,对这种关系强制执行级联删除的唯一方法是直接在 SQL 数据库上使用 SQL 触发器或迭代集合并删除所有子对象。

Hope it helps!希望能帮助到你!

暂无
暂无

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

相关问题 MVC 4实体框架同一表的两个外键导致循环或多个级联路径 - MVC 4 Entity Framework Two Foreign Keys to the Same Table Cause cycles or multiple cascade paths 实体框架多个复合键,在删除时级联 - Entity framework multiple composite keys, cascade on delete 迁移不适用于 ON CASCADE DELETE 行为的所有外键 - Migration doesn't apply on all foreign keys the ON CASCADE DELETE behavior 同一张表上有多个外键。 如何重新设计我的 model? - Multiple foreign keys on the same table. How to redesign my model? 实体框架同一表上的多个外键映射不正确 - Entity Framework Multiple Foreign Keys on same table not mapping correctly 创建多个自引用外键到同一个表 - Creating multiple self-referencing foreign keys to the same table 指向实体框架中相同表的多个外键是无法区分的 - Multiple foreign keys pointing to same table in Entity Framework are indistinguisible 删除级联上的同一个表一对多 - Same table one to many on delete cascade 在表table上引入FOREIGN KEY约束键可能会导致循环或多个级联路径。 指定ON DELETE…错误 - Introducing FOREIGN KEY constraint key on table table may cause cycles or multiple cascade paths. Specify ON DELETE … Error 在表 'XY' 上引入 FOREIGN KEY 约束 'FK_XY' 可能会导致循环或多个级联路径。 指定 ON DELETE NO ACTION - Introducing FOREIGN KEY constraint 'FK_XY' on table 'XY' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM