引入外键约束可能会导致循环或多个级联路径-为什么?我和这件事斗争了一段时间了,我不太明白到底发生了什么事。我有一个卡片实体,其中包含侧(通常2)-和卡片和两边都有一个舞台。我使用的是EF CodeFirst迁移,但是这个错误导致迁移失败:在表“侧”上引入外键约束‘fk_dbo.ides_dbo.Cards_CardId’可能会导致循环或多个级联路径。指定“删除不操作”或“更新不操作”,或修改其他外键约束。这是我的卡片实体:public class Card{
public Card()
{
Sides = new Collection<Side>();
Stage = Stage.ONE;
}
[Key]
[Required]
public virtual int CardId { get; set; }
[Required]
public virtual Stage Stage { get; set; }
[Required]
[ForeignKey("CardId")]
public virtual ICollection<Side> Sides { get; set; }}这是我的侧实体:public class Side{
public Side()
{
Stage = Stage.ONE;
}
[Key]
[Required]
public virtual int SideId { get; set; }
[Required]
public virtual Stage Stage { get; set; }
[Required]
public int CardId { get; set; }
[ForeignKey("CardId")]
public virtual Card Card { get; set; }}这是我的舞台实体:public class Stage{
// Zero
public static readonly Stage ONE = new Stage(new TimeSpan(0, 0, 0), "ONE");
// Ten seconds
public static readonly Stage TWO = new Stage(new TimeSpan(0, 0, 10), "TWO");
public static IEnumerable<Stage> Values
{
get
{
yield return ONE;
yield return TWO;
}
}
public int StageId { get; set; }
private readonly TimeSpan span;
public string Title { get; set; }
Stage(TimeSpan span, string title)
{
this.span = span;
this.Title = title;
}
public TimeSpan Span { get { return span; } }}奇怪的是,如果我在舞台课上加上以下内容: public int? SideId { get; set; }
[ForeignKey("SideId")]
public virtual Side Side { get; set; }迁移成功运行。如果我打开SSM,看看这些表,我可以看到Stage_StageId已添加到Cards(如预期/所需)Sides不包含引用Stage(未预期)。如果我再加上 [Required]
[ForeignKey("StageId")]
public virtual Stage Stage { get; set; }
public int StageId { get; set; }到我的旁听课上,我明白了StageId列添加到我的Side桌子。这是可行的,但现在我的整个应用程序中,任何引用Stage包含SideId在某些情况下是完全不相关的。我只想把我的Card和Side实体aStage属性,如果可能的话,在不污染带有引用属性的Stage类的情况下,..我做错什么了?
3 回答
慕码人2483693
TA贡献1860条经验 获得超9个赞
Stage
Stage
Stage
删除将直接级联到 Side
删除将直接级联到 Card
因为 Card
和 Side
在默认情况下启用级联删除所需的一对多关系,然后它将从 Card
到 Side
Stage
Side
Stage
[Required]
Stage
modelBuilder.Entity<Card>() .HasRequired(c => c.Stage) .WithMany() .WillCascadeOnDelete(false);modelBuilder.Entity<Side>() .HasRequired(s => s.Stage) .WithMany() .WillCascadeOnDelete(false);
一只名叫tom的猫
TA贡献1906条经验 获得超3个赞
[ForeignKey("StageId")]public virtual Stage Stage { get; set; }public int? StageId { get; set; }
牛魔王的故事
TA贡献1830条经验 获得超3个赞
protected override void OnModelCreating(ModelBuilder modelBuilder) { foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys())) { relationship.DeleteBehavior = DeleteBehavior.Restrict; } ..... rest of the code.....
- 3 回答
- 0 关注
- 595 浏览
添加回答
举报
0/150
提交
取消