[英]Introducing FOREIGN KEY constraint ... may cause cycles or multiple cascade paths - Entity Framework
I'm new to Entity Framework and I've tried to fix this for multiple hours now but I'm now at a complete loss.我是 Entity Framework 的新手,我已经尝试了几个小时来解决这个问题,但我现在完全不知所措。
Could anybody be so kind to point out what I'm missing here?有人能这么好心地指出我在这里缺少什么吗?
I get this error when I try to run Update-Database:当我尝试运行 Update-Database 时出现此错误:
Introducing FOREIGN KEY constraint 'FK_ContainerActies_Locaties_LocatieId' on table 'ContainerActies' may cause cycles or multiple cascade paths.
在表 'ContainerActies' 上引入 FOREIGN KEY 约束 'FK_ContainerActies_Locaties_LocatieId' 可能会导致循环或多个级联路径。 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 约束。 Could not create constraint or index.
无法创建约束或索引。 See previous errors.
请参阅以前的错误。
I try to turn the cascade delete off in OnModelCreating:我尝试在 OnModelCreating 中关闭级联删除:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ContainerActie>()
.HasOne(c => c.Locatie)
.WithMany(c => c.ContainerActies)
.HasForeignKey(c => c.LocatieId)
.OnDelete(DeleteBehavior.Restrict);
}
But it doesn't seem to have any effect.但它似乎没有任何效果。
public class ContainerActie : SQLModel
{
[Category("DB")]
[Key]
public int ContainerActieId { get; set; }
[Category("Info")]
public Container Container { get; set; }
[Category("Info")]
public Actie Actie { get; set; }
[Category("Info")]
public Relatie Relatie { get; set; }
[Category("Info")]
public int LocatieId { get; set; }
[Category("Info")]
public Locatie Locatie { get; set; }
[Category("Info")]
public DateTime DateTime { get; set; }
}
public class Locatie : SQLModel
{
[Category("DB")]
[Key]
public int LocatieId { get; set; }
[Category("Info")]
[Required]
public string Naam { get; set; }
[Category("Info")]
public string? Adres { get; set; }
[Category("Info")]
public string? Gemeente { get; set; }
[Category("Info")]
public string? Postcode { get; set; }
[Category("Info")]
public string? Tel { get; set; }
[Category("Info")]
public string? Commentaar { get; set; }
[Category("Info")]
public List<ContainerActie>? ContainerActies { get; set; }
}
这是因为外键LocatieId
不可为空,如果该键不可为空,则必须删除相关对象,循环关系不允许这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.