简体   繁体   English

FOREIGN KEY 约束可能会导致循环或多个级联路径。 ef core 3(根本无法创建数据库)

[英]FOREIGN KEY constraint may cause cycles or multiple cascade paths. ef core 3 (cannot create a DB at all)

I am getting the multiple cascade path error.我收到多个级联路径错误。

My models are 0..1 to 0..1 Or at least that is what i am trying to achieve.我的模型是 0..1 到 0..1 或者至少这是我想要实现的。

My models look like this:我的模型看起来像这样:

public class House
{
   public Guid Id { get; set; }

   [ForeignKey("User")]
   public User Tenant { get; set; }

public class User
{
    public House Home { get; set; }

    public Guid Id { get; set; }

I have also this:我也有这个:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<House>()
        .HasOne(p => p.Tenant)
        .WithOne(t => t.Home)
        .OnDelete(DeleteBehavior.Restrict);
}   

The error is not that i get an error when i delete a object, it simply will not create the DB at all.错误不是我在删除对象时收到错误,它根本不会创建数据库。

I have already been told that this has been solved here:我已经被告知这已经在这里解决了:

Entity Framework Core cascade delete one to many relationship Entity Framework Core 级联删除一对多关系

However i am afraid to say that it does not appear to really address my issue, or if it does i do not understand how it solves it.但是,我不敢说它似乎并没有真正解决我的问题,或者如果确实如此,我不明白它是如何解决的。

Any help would be greatly appreciated.任何帮助将不胜感激。

//update //更新

Just to clarify.只是为了澄清。 There are many users/tenants and many houses/homes.有许多用户/租户和许多房屋/家庭。 Each of the tenants can have a house but does not always.每个租户都可以拥有一所房子,但并非总是如此。 Each of the homes can have a tenant but does not always have one.每个家庭都可以有一个租户,但并不总是一个租户。

Try a very simple thing just to be sure when you start getting errors :尝试一个非常简单的事情,以确保何时开始出现错误:

public class User{
   public Guid Id{get; set;}
   public string UserName{ get; set;}
}


public class House{
      public Guid Id {get; set;}
      public string StreetName {get; set;}
      public UserId {get; set;} //this should generate a foreign key to the User table
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<House>().Property(c => c.UserId).IsRequired(false);
}

This should work to create houses that doesn't necessarely requires Users to exist.这应该可以创建不需要用户存在的房屋。

So 1 house can have only 1 tenant.因此,1 所房子只能有 1 个租户。 And 1 user can stay in only 1 home.并且 1 个用户只能留在 1 个家中。 Your relationship is 1 to 1. In this case, you can put foreign key in either table, but only 1 table.你们的关系是 1 对 1。在这种情况下,您可以在任一表中放置外键,但只能放置 1 个表。 Then, you don't have issue of cycle cascade.然后,您就没有循环级联的问题。

I suggest to put HouseId in User class, so that you can extend your app to allow 1 house to have many tenants easily later.我建议将 HouseId 放在 User 类中,以便您可以扩展您的应用程序,以便以后轻松地让 1 个房子拥有多个租户。

Here, one foreign key is declared :这里声明了一个外键:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<House>()
        .HasOne(p => p.Tenant)
        .WithOne(t => t.Home)
        .OnDelete(DeleteBehavior.Restrict);
}  

House.Tenant is linked to User.Home . House.Tenant链接到User.Home The tenant is the resident.承租人是住户。 Not what you want.不是你想要的。

You need declare two foreign key distinct :您需要声明两个不同的外键:

Fluent syntax流畅的语法

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>().HasOne(u => u.Home).WithOne();
    modelBuilder.Entity<House>().HasOne(h => h.Tenant).WithOne();
}

Attribute syntax属性语法

public class House
{
    public Guid Id { get; set; }
    [ForeignKey("TenantId")]
    public User Tenant { get; set; }
}

public class User
{
    [ForeignKey("HomeId")]
    public House Home { get; set; }
    public Guid Id { get; set; }
}

It turns out that Ivan was correct in one of his comments above.事实证明,伊万在上面的评论之一中是正确的。 The issue was not actually in the code itself.问题实际上并不在代码本身。 In fact even simpler code will work in this case.事实上,在这种情况下,甚至更简单的代码也能工作。 The error was caused by the migrations.该错误是由迁移引起的。 I had to remove all migrations and start from scratch.我不得不删除所有迁移并从头开始。 Clearly some of the older model designs clashed with this new addition in a way that it was simply not possible to move forward.很明显,一些较旧的模型设计与这个新增加的东西发生了冲突,以至于根本不可能向前发展。 The databases had to be recreated from scratch.必须从头开始重新创建数据库。

Thank you everyone for your help.感谢大家的帮助。 Especially Ivan and David.尤其是伊万和大卫。

Here are the simple models that do what i needed to do:这是做我需要做的事情的简单模型:

public class Home
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Guid? TenantId { get; set; } 
    public Tenant Tenant { get; set; } 
}

public class Tenant
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Home Home { get; set; } 
}

I do not even have to override the OnModelCreating.我什至不必覆盖 OnModelCreating。

暂无
暂无

声明:本站的技术帖子网页,遵循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 EF FOREIGN KEY约束可能会导致循环或多个级联路径 - EF FOREIGN KEY constraint may cause cycles or multiple cascade paths 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 引入FOREIGN KEY约束可能会导致EF Core中的循环或多个级联路径 - Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths in EF Core 在表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 EF-代码优先FOREIGN KEY约束可能会导致循环或多个级联路径 - EF - Code First FOREIGN KEY constraint may cause cycles or multiple cascade paths 外键约束可能导致循环或多个级联路径 ASP.NET Core MVC - Foreign key constraint may cause cycles or multiple cascade paths ASP.NET Core MVC EF Core - 可能导致循环或多个级联路径 - EF Core - may cause cycles or multiple cascade paths 在表上引入外键约束可能会导致循环或多个级联路径 - Introducing Foreign key Constraint on table may cause cycles or multiple cascade paths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM