在 table 上引入 FOREIGN KEY 约束可能会导致引用自身的 class 出现循环或多个级联路径
Introducing FOREIGN KEY constraint on table may cause cycles or multiple cascade paths for a class that references itself
我在尝试更新包含引用自身的 class 的数据库时收到错误 Introducing FOREIGN KEY constraint 'FK_Company_Company_ParentId' on table 'Company' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
。
Class 产生问题:
public class Company
{
public Country Country { get; set; }
[Required]
public int CountryId { get; set; }
[Required]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public Company Parent { get; set; }
public int ParentId { get; set; }
}
我的 dbcontext 看起来像这样:
using System;
using System.Collections.Generic;
using System.Text;
using ArtGalleries.Domain;
using Microsoft.EntityFrameworkCore;
namespace ArtGalleries.Data
{
public class ArtGalleriesContext:DbContext
{
public ArtGalleriesContext(DbContextOptions<ArtGalleriesContext> options):base(options)
{
}
public DbSet<ArtItem> ArtItems { get; set; }
public DbSet<Artist> Artists { get; set; }
public Company Companies { get; set; }
public DbSet<Floor> Floors { get; set; }
public Location Locations { get; set; }
public DbSet<Room> Rooms { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserLocation>()
.HasKey(u => new {u.UserId, u.LocationId});
//modelBuilder.Entity().
//modelBuilder.Entity<Company>()
// .HasOne(c => c.Company)
// .WithOne(pc => pc.Company)
// .HasForeignKey(fk => fk.ParentId);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(
"Server=(localdb)\mssqllocaldb;Database=GalleriesDB;Trusted_Connection=True;");
}
}
}
}
注意:评论的文字是我试图解决这个问题,但没有结果,因为我遇到了语法错误。
对于 EF Core 中的自引用,您应该将 ParentId
设置为 Nullable 并使用 ForeignKey 来配置哪个 属性 应该用作外键 属性对于如下所示的给定关系:
public class Company
{
[Required]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public int? ParentId { get; set; }
[ForeignKey("ParentId")]
public Company Parent { get; set; }
[Required]
public int CountryId { get; set; }
[ForeignKey("CountryId ")]
public Country Country { get; set; }
}
我在尝试更新包含引用自身的 class 的数据库时收到错误 Introducing FOREIGN KEY constraint 'FK_Company_Company_ParentId' on table 'Company' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
。
Class 产生问题:
public class Company
{
public Country Country { get; set; }
[Required]
public int CountryId { get; set; }
[Required]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public Company Parent { get; set; }
public int ParentId { get; set; }
}
我的 dbcontext 看起来像这样:
using System;
using System.Collections.Generic;
using System.Text;
using ArtGalleries.Domain;
using Microsoft.EntityFrameworkCore;
namespace ArtGalleries.Data
{
public class ArtGalleriesContext:DbContext
{
public ArtGalleriesContext(DbContextOptions<ArtGalleriesContext> options):base(options)
{
}
public DbSet<ArtItem> ArtItems { get; set; }
public DbSet<Artist> Artists { get; set; }
public Company Companies { get; set; }
public DbSet<Floor> Floors { get; set; }
public Location Locations { get; set; }
public DbSet<Room> Rooms { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserLocation>()
.HasKey(u => new {u.UserId, u.LocationId});
//modelBuilder.Entity().
//modelBuilder.Entity<Company>()
// .HasOne(c => c.Company)
// .WithOne(pc => pc.Company)
// .HasForeignKey(fk => fk.ParentId);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(
"Server=(localdb)\mssqllocaldb;Database=GalleriesDB;Trusted_Connection=True;");
}
}
}
}
注意:评论的文字是我试图解决这个问题,但没有结果,因为我遇到了语法错误。
对于 EF Core 中的自引用,您应该将 ParentId
设置为 Nullable 并使用 ForeignKey 来配置哪个 属性 应该用作外键 属性对于如下所示的给定关系:
public class Company
{
[Required]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public int? ParentId { get; set; }
[ForeignKey("ParentId")]
public Company Parent { get; set; }
[Required]
public int CountryId { get; set; }
[ForeignKey("CountryId ")]
public Country Country { get; set; }
}