EF Core - may cause cycles or multiple cascade paths
For others that finds this question, this is enough:
modelBuilder.Entity<User>()
.HasOne(u => u.Suburb)
.WithMany(s => s.Users)
.OnDelete(DeleteBehavior.Restrict);
If you don't have want a list property in Suburb
with Users
you can do it like this as well:
modelBuilder.Entity<User>()
.HasOne(u => u.Suburb)
.WithMany()
.OnDelete(DeleteBehavior.Restrict);
The error already says what you need to do. Specify what it must do when there is an action. You should add the .OnDelete()
method to each foreign key definition.
modelBuilder.Entity<Tenant>()
.HasOne<User>(s => s.User)
.WithMany(ta => ta.Tenants)
.HasForeignKey(u => u.UserId)
.OnDelete(DeleteBehavior.Restrict);
For further information please read https://www.learnentityframeworkcore.com/configuration/fluent-api/ondelete-method
Your User
entity ForeignFey
Fluent API configuration should be as follows:
modelBuilder.Entity<User>()
.HasOne<Suburb>(s => s.Suburb)
.WithMany(u => u.Users)
.HasForeignKey(u => u.SuburbId)
.IsRequired(false);
.OnDelete(DeleteBehavior.Restrict); // <-- Here it is