我想在菜单项和餐厅之间创建一种关系 - 每个菜单项可以有一个餐厅,而餐厅可以有多个菜单项,我想将其设置为一对多关系,但是在'update-database' 命令我收到错误:“在表 'MenuItems' 上引入外键约束 'FK_MenuItems_Restaurants_RestaurantId' 可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他外键约束。 无法创建约束或索引。请参阅以前的错误。”
有什么解决办法吗?
public class Restaurant
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string Address { get; set; } = string.Empty;
public string PhoneNumber { get; set; } = string.Empty;
public List<MenuItem> MenuItems { get; set; } = new List<MenuItem>();
}
public class MenuItem
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public Restaurant Restaurant { get; set; }
public int RestaurantId { get; set; }
}
我在 DataContext 类中像这样配置它:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Restaurant>()
.HasOne(k => k.Owner)
.WithMany(k => k.Restaurants)
.HasForeignKey(k => k.OwnerId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<MenuItem>()
.HasOne(k => k.Restaurant)
.WithMany(k => k.MenuItems)
.HasForeignKey(k => k.RestaurantId)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
我会删除
HasOne
并将其替换为 HasRequired
并映射它的 Id。实体将自行制作外键。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Restaurant>()
.HasRequired(k => k.Owner)
.WithMany(k => k.Restaurants)
.Map(k => k.MapKey("OwnerId"))
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<MenuItem>()
.HasRequired(k => k.Restaurant)
.WithMany(k => k.MenuItems)
.Map(k => k.MapKey("RestaurantId"))
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
我认为问题是由
Restaurant
实体和 MenuItem
实体上的级联删除操作的组合引起的。要解决此问题,您应该调整一个或两个关系的级联删除行为。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Restaurant>()
.HasOne(k => k.Owner)
.WithMany(k => k.Restaurants)
.HasForeignKey(k => k.OwnerId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<MenuItem>()
.HasOne(k => k.Restaurant)
.WithMany(k => k.MenuItems)
.HasForeignKey(k => k.RestaurantId)
.OnDelete(DeleteBehavior.Restrict); // Change this to Restrict
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}