在ef core中你可能會設計這樣一個實體:ide
public class Customer : Entity,IMustHaveTenant, IHasCreationTime { public Customer() {
this.ChildrenCustomers = new List<Customer>(); }
//……
/// <summary> /// 父級顧客 /// </summary> public int? ParentCustomerId { get; set; }
#region 導航屬性
/// <summary> /// 父顧客 /// </summary> public virtual Customer ParentCustomer { get; set; } /// <summary> /// 孩子顧客 /// </summary> public virtual ICollection<Customer> ChildrenCustomers { get; set; } #endregion }
意思就是:一個顧客可能有上級顧客,同時也可能有多個孩子顧客。ui
當咱們在進行數據遷移的時候會報錯:this
將 FOREIGN KEY 約束 '******' 引入表 'Customers' 可能會致使循環或多重級聯路徑。請指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其餘 FOREIGN KEY 約束。
沒法建立約束或索引。請參閱前面的錯誤。spa
你可能須要這樣配置來解決這個問題:設計
protected override void OnModelCreating(ModelBuilder modelBuilder) { string tableNamePrefix = "WXShop_"; var customer = modelBuilder.Entity<Customer>(); customer.ToTable(tableNamePrefix + "Customers"); customer .HasMany(t => t.ChildrenCustomers) .WithOne(t => t.ParentCustomer) .HasForeignKey(t => t.ParentCustomerId) .OnDelete(DeleteBehavior.Restrict); base.OnModelCreating(modelBuilder); }