C# 使用级联更新和删除将多个外键复制到同一个表
我正在尝试为一个实体添加多个外键,该实体将通过级联更新和删除连接到同一个表 所以我有系列和参数实体:C# 使用级联更新和删除将多个外键复制到同一个表,c#,entity-framework,ef-code-first,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; } pub
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; }
}
到
模型创建,但不是我想要的。
是否可能通过级联更新/删除对同一个表进行多重引用?基本上,SQL Server不允许在内部关系上创建级联操作–当级联路径从表A中的列col1转到表A中的列col2时(A=>A) 因此,对这种关系强制执行级联删除的唯一方法是直接在SQL数据库上使用SQL触发器,或者迭代集合并删除所有子对象
希望有帮助 是否要在每次删除一个参数类型时删除一个系列?这就是为什么会出现错误。
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);
}
}
.WillCascadeOnDelete(true);
.WillCascadeOnDelete(false);