简体   繁体   English

引入 FOREIGN KEY 约束...可能导致循环或多个级联路径 - 实体框架

[英]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.

相关问题 SQL 错误:引入 FOREIGN KEY 约束可能会导致循环或多个级联路径。 实体框架核心 - SQL Error: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Entity Framework Core 实体框架:在表 '' 上引入 FOREIGN KEY 约束 '' 可能会导致循环或多个级联路径 - Entity Framework: Introducing FOREIGN KEY constraint '' on table '' may cause cycles or multiple cascade paths 实体框架C#在表Y中引入外键X可能会导致循环或多个级联路径 - Entity Framework C# Introducing A Foreign Key X In Table Y May Cause Cycles OR Multiple Cascade Paths 实体框架,外键约束可能会导致循环或多个级联路径 - Entity Framework, Foreign key constraint may cause cycles or multiple cascade paths 当不需要属性时,引入外键约束可能会导致循环或多个级联路径 - Introducing Foreign Key Constraint may cause cycles or multiple cascade paths when property is not required 错误:引入FOREIGN KEY约束可能会导致循环或多个级联路径-为什么? - Error: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why? ef核心2-在表&#39;Y&#39;上引入FOREIGN KEY约束&#39;X&#39;可能会导致循环或多个级联路径 - ef core 2 - Introducing FOREIGN KEY constraint 'X' on table 'Y' may cause cycles or multiple cascade paths 在表上引入外键约束可能会导致循环或多个级联路径 - Introducing Foreign key Constraint on table may cause cycles or multiple cascade paths 在表“模型”上引入FOREIGN KEY约束“列”可能会导致循环或多个级联路径 - Introducing FOREIGN KEY constraint 'Column' on table 'Model' may cause cycles or multiple cascade paths 引入FOREIGN KEY约束可能会导致EF Core中的循环或多个级联路径 - Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths in EF Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM