Представляем FOREIGN Ограничение KEY «FK_User_Suburb_SuburbId» в таблице «Пользователь» может вызывать циклы или несколько каскадных путей. Укажите ON DELETE NO ACTION или ON UPDATE NO ACTION или измените другие ограничения FOREIGN KEY.
Не удалось создать ограничение или индекс. См. предыдущие ошибки.
Вот мой КАТАЛОГСотекст:
Код: Выделить всё
using JobsLedger.CATALOG.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
namespace JobsLedger.CATALOG
{
public class CATALOGContext : DbContext
{
public DbSet Tenants { get; set; }
public DbSet Users { get; set; }
public DbSet Roles { get; set; }
public DbSet States { get; set; }
public DbSet Suburbs { get; set; }
public DbSet Counters { get; set; }
public CATALOGContext(DbContextOptions options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
entity.Relational().TableName = entity.DisplayName();
}
// User
modelBuilder.Entity().Property(u => u.UserName).IsRequired().HasMaxLength(50);
modelBuilder.Entity().Property(u => u.UserFirstName).IsRequired().HasMaxLength(100);
modelBuilder.Entity().Property(u => u.UserLastName).IsRequired().HasMaxLength(100);
modelBuilder.Entity().Property(u => u.Email).IsRequired().HasMaxLength(200);
modelBuilder.Entity().Property(u => u.HashedPassword).IsRequired().HasMaxLength(200);
modelBuilder.Entity().Property(u => u.Salt).IsRequired().HasMaxLength(200);
modelBuilder.Entity()
.HasOne(s => s.Suburb)
.WithMany(u => u.Users)
.HasForeignKey(u => u.SuburbId)
.IsRequired(false);
// Role
modelBuilder.Entity().Property(r => r.Name).IsRequired().HasMaxLength(50);
modelBuilder.Entity()
.HasOne(u => u.User)
.WithOne(r => r.Role)
.HasForeignKey(u => u.RoleId);
// TenantAccount
modelBuilder.Entity().Property(t => t.TenantNo).HasMaxLength(20);
modelBuilder.Entity().Property(t => t.Company).HasMaxLength(100).IsRequired();
modelBuilder.Entity().Property(t => t.ContactLastName).HasDefaultValue(false).IsRequired();
modelBuilder.Entity().Property(t => t.Email).HasMaxLength(500).IsRequired();
modelBuilder.Entity().Property(t => t.MobilePhone).HasMaxLength(20).IsRequired();
modelBuilder.Entity().Property(t => t.OfficePhone).HasMaxLength(20);
modelBuilder.Entity().Property(t => t.CompanyEmail).HasMaxLength(500);
modelBuilder.Entity().Property(t => t.Address1).HasMaxLength(500);
modelBuilder.Entity().Property(t => t.Address2).HasMaxLength(500);
modelBuilder.Entity().Property(t => t.ABN).HasMaxLength(14);
modelBuilder.Entity().Property(t => t.Database).HasMaxLength(100).IsRequired();
modelBuilder.Entity().Property(t => t.IsLocked).HasDefaultValue(false);
modelBuilder.Entity()
.HasOne(s => s.User)
.WithMany(ta => ta.Tenants)
.HasForeignKey(u => u.UserId);
modelBuilder.Entity()
.HasOne(s => s.Suburb)
.WithMany(ta => ta.Tenants)
.HasForeignKey(ta => ta.SuburbId);
// State
modelBuilder.Entity().Property(s => s.StateShortName).HasMaxLength(3).IsRequired();
modelBuilder.Entity().Property(s => s.StateName).HasMaxLength(30).IsRequired();
// Suburb
modelBuilder.Entity().Property(s => s.SuburbName).HasMaxLength(3).IsRequired();
modelBuilder.Entity().Property(s => s.PostCode).HasMaxLength(30).IsRequired();
modelBuilder.Entity()
.HasOne(s => s.State)
.WithMany(su => su.Suburbs)
.HasForeignKey(st => st.StateId);
}
}
}
Код: Выделить всё
...
public int? SuburbId { get; set; }
public Suburb Suburb { get; set; }
public int RoleId { get; set; }
public Role Role { get; set; }
public virtual ICollection Tenants { get; set; }
Интересно, может ли кто-нибудь объяснить, почему миграция работает, но когда я пытаюсь развернуть базу данных, возникает ошибка, описанная выше.
Саймон
Подробнее здесь: https://stackoverflow.com/questions/559 ... cade-paths