我知道,我知道,已经存在很多这样的问题,但我不知道如何在我的场景中解决这个问题。
我有一个客户,一个有卖家,客户不一定是卖家,但所有卖家都是客户。
在我的订单中,我有一个客户和一个卖家,但是卖家里面已经有一个客户
这是我的客户实体
public class Customer : BaseEntity
{
public Customer(Name name, DateTime? birthDay, Email email, string password, List<CreditDebitCard> creditDebitCards = null)
{
[...]
}
public Name Name { get; private set; }
public DateTime? BirthDay { get; private set; }
public Email Email { get; private set; }
public string Password { get; private set; }
public int? CreditDebitCardId { get; private set; }
public ICollection<CreditDebitCard> CreditDebitCards => _CreditDebitCards;
public IEnumerable<Order> Orders { get; private set; }
[...]
}
我的卖家实体
public class Seller : BaseEntity
{
public Seller(string schoolName, string cardIdPath, string identityNumber, int customerId)
{
[...]
}
public string SchoolName { get; private set; }
public string CardIdPath { get; private set; }
public string IdentityNumber { get; private set; }
public Customer Customer { get; private set; }
public int CustomerId { get; private set; }
public IEnumerable<Order> Orders { get; private set; }
}
我的订单实体
public class Order : BaseEntity
{
public Order(IList<OrderItem> orderItems, int customerId, int sellerId)
{
[...]
}
#region Fields
private IList<OrderItem> _orderItems;
#endregion
#region Properties
public int SellerId { get; private set; }
public Seller Seller { get; private set; }
public int CustomerId { get; private set; }
public Customer Customer { get; private set; }
public EOrderStatus Status { get; private set; }
public ICollection<OrderItem> OrderItems
{
get { return _orderItems; }
private set { _orderItems = new List<OrderItem>(value); }
}
public decimal Total => GetTotal();
#endregion
[...]
}
我的订单地图
public static class OrderMap
{
public static EntityTypeBuilder<Order> Map(this EntityTypeBuilder<Order> cfg)
{
cfg.ToTable("ORDER");
cfg.HasKey(x => x.Id);
cfg.Property(x => x.CreationDate).IsRequired();
cfg.Property(x => x.IsActive).IsRequired();
//cfg.Property(x => x.Discount).HasColumnType("money");
cfg.Property(x => x.Status).IsRequired();
cfg.HasMany(x => x.OrderItems);
cfg.HasOne(x => x.Customer);
cfg.HasOne(x => x.Seller).WithMany(b => b.Orders).OnDelete(DeleteBehavior.ClientSetNull);
return cfg;
}
}
我已经添加了迁移,但是当我设置“更新数据库”时,我收到此错误消息:
在表“订单”上引入外键约束“FK_Order_Seller_SellerId”可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。无法创建约束或索引。请参阅以前的错误。
这是一个 .netcore2.0 项目,我正在使用 ef-core 2