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

c# entity-framework-core

Question

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.

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

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.

1
0
3/9/2020 10:40:25 AM

Accepted Answer

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.

0
3/11/2020 11:18:16 AM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow