Thắc mắc về EF Core

Hiện tại em đang gặp phải lỗi sau khi cấu hình mối quan hệ trong database. Cụ thể khi Add-Migration thì nó báo lỗi: Introducing FOREIGN KEY constraint ‘FK_InventoryLogEntries_Items_ItemId’ on table ‘InventoryLogEntries’ may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index.

Code:

public class InventoryLogEntry : Entity, IAggregateRoot
{
    public int ItemId { get; private set; }              // ForeignKey
    public int ItemLotId { get; private set; }           // ForeignKey
    public DateTime Timestamp { get; private set; }
    public double BeforeQuantity { get; private set; }
    public double ChangedQuantity { get; private set; }
    public Item Item { get; private set; }
    public ItemLot ItemLot { get; private set; }

    private InventoryLogEntry()
    {

    }
    public InventoryLogEntry(int itemId, int itemLotId, DateTime timestamp, 
        double beforeQuantity, double changedQuantity)
    {
        ItemId = itemId;
        ItemLotId = itemLotId;
        Timestamp = timestamp;
        BeforeQuantity = beforeQuantity;
        ChangedQuantity = changedQuantity;
    }
}
public class ItemLot : Entity, IAggregateRoot
{
    public string LotId { get; private set; }
    public int LocationId { get; private set; }             // ForeignKey
    public int ItemId { get; private set; }                 // ForeignKey
    public bool IsIsolated { get; private set; } = false;
    public double Quantity { get; private set; }
    public double? SublotSize { get; private set; }
    public string PurchaseOrderNumber { get; private set; }
    public DateTime ProductionDate { get; private set; }
    public DateTime ExpirationDate { get; private set; }
    public Location Location { get; private set; }
    public Item Item { get; private set; }

    public ItemLot(string lotId, int locationId, int itemId, double quantity, 
        double? sublotSize, string purchaseOrderNumber, DateTime productionDate, DateTime expirationDate)
    {
        LotId = lotId;
        LocationId = locationId;
        ItemId = itemId;
        Quantity = quantity;
        SublotSize = sublotSize;
        PurchaseOrderNumber = purchaseOrderNumber;
        ProductionDate = productionDate;
        ExpirationDate = expirationDate;
    }
}

public class Item : Entity, IAggregateRoot
{
    public string ItemId { get; private set; }
    public string ItemClassId { get; private set; }             // ForeignKey
    public string UnitName { get; private set; }                // ForeignKey
    public string ItemName { get; private set; }
    public double MinimumStockLevel { get; private set; }
    public double Price { get; private set; }
    public Unit Unit { get; private set; }
    public ItemClass ItemClass { get; private set; }

    public Item(string itemId, string itemClassId, string unitName, string itemName,
        double minimumStockLevel, double price)
    {
        ItemId = itemId;
        ItemClassId = itemClassId;
        UnitName = unitName;
        ItemName = itemName;
        MinimumStockLevel = minimumStockLevel;
        Price = price;
    }

    public void Update(Unit unit, double minimumStockLevel, double price)
    {
        Unit = unit;
        MinimumStockLevel = minimumStockLevel;
        Price = price;
    }
public class ItemClass
{
    public string ItemClassId { get; private set; }
    public List<Item> Items { get; private set; } = new List<Item>();
    public ItemClass(string itemClassId) 
    {
        ItemClassId = itemClassId;
    }
}
public class Unit
{
    public string UnitName { get; private set; }
    public Unit(string unitName) 
    {
        UnitName = unitName;
    }
}
public class Location : Entity
{
    public string LocationId { get; private set; }
    public List<ItemLot> ItemLots { get; private set; }

    public Location(string locationId)
    {
        LocationId = locationId;
        ItemLots = new List<ItemLot>();
    }
}
public abstract class Entity
{
    int? _requestedHashCode;
    int _Id;
    public virtual int Id
    {
        get
        {
            return _Id;
        }
        protected set
        {
            _Id = value;
        }
    }

    private List<INotification>? _domainEvents;
    public IReadOnlyCollection<INotification>? DomainEvents => _domainEvents?.AsReadOnly();

    public void AddDomainEvent(INotification eventItem)
    {
        _domainEvents ??= new List<INotification>();
        _domainEvents.Add(eventItem);
    }

    public void RemoveDomainEvent(INotification eventItem)
    {
        _domainEvents?.Remove(eventItem);
    }

    public void ClearDomainEvents()
    {
        _domainEvents?.Clear();
    }

    public bool IsTransient()
    {
        return this.Id == default;
    }

    public override bool Equals(object? obj)
    {
        if (obj == null || obj is not Entity)
            return false;

        if (Object.ReferenceEquals(this, obj))
            return true;

        if (this.GetType() != obj.GetType())
            return false;

        Entity item = (Entity)obj;

        if (item.IsTransient() || this.IsTransient())
            return false;
        else
            return item.Id == this.Id;
    }

    public override int GetHashCode()
    {
        if (!IsTransient())
        {
            if (!_requestedHashCode.HasValue)
                _requestedHashCode = this.Id.GetHashCode() ^ 31; // XOR for random distribution (http://blogs.msdn.com/b/ericlippert/archive/2011/02/28/guidelines-and-rules-for-gethashcode.aspx)

            return _requestedHashCode.Value;
        }
        else
            return base.GetHashCode();

    }
    public static bool operator ==(Entity left, Entity right)
    {
        if (Object.Equals(left, null))
            return (Object.Equals(right, null));
        else
            return left.Equals(right);
    }

    public static bool operator !=(Entity left, Entity right)
    {
        return !(left == right);
    }
}
public class InventoryLogEntryEntityTypeConfiguration : IEntityTypeConfiguration<InventoryLogEntry>
{
    public void Configure(EntityTypeBuilder<InventoryLogEntry> builder)
    {
        builder.HasKey(log => log.Id);
        builder.Ignore(d => d.DomainEvents);
        builder.Property(log => log.Timestamp).IsRequired();
        builder.Property(log => log.BeforeQuantity).IsRequired();
        builder.Property(log => log.ChangedQuantity).IsRequired();

        builder.HasOne(log => log.Item).WithMany().HasForeignKey(log => log.ItemId);
        builder.HasOne(log => log.ItemLot).WithMany().HasForeignKey(log => log.ItemLotId));
    }
}
public class ItemLotEntityTypeConfiguration : IEntityTypeConfiguration<ItemLot>
{
    public void Configure(EntityTypeBuilder<ItemLot> builder)
    {
        builder.HasKey(l => l.Id);
        builder.HasIndex(l => l.LotId).IsUnique();
        builder.Property(l => l.IsIsolated).IsRequired();
        builder.Property(l => l.Quantity).IsRequired();
        builder.Property(l => l.SublotSize).IsRequired();
        builder.Property(l => l.PurchaseOrderNumber).IsRequired();
        builder.Property(l => l.ProductionDate).IsRequired();
        builder.Property(l => l.ExpirationDate).IsRequired();
        builder.Ignore(d => d.DomainEvents);

        builder.HasOne(i => i.Item).WithMany().HasForeignKey(i => i.ItemId);
    }
}
public class ItemEntityTypeConfiguration : IEntityTypeConfiguration<Item>
{
    public void Configure(EntityTypeBuilder<Item> builder)
    {
        builder.HasKey(i => i.Id);
        builder.HasIndex(i => i.ItemId).IsUnique();
        builder.Property(i => i.ItemName).IsRequired();
        builder.Property(i => i.MinimumStockLevel).IsRequired();
        builder.Property(i => i.Price).IsRequired();
        builder.Ignore(d => d.DomainEvents);

        builder.HasOne(u => u.Unit).WithMany().HasForeignKey(i => i.UnitName);
        builder.HasOne(i => i.ItemClass).WithMany(i => i.Items).HasForeignKey(i => i.ItemClassId);
    }
}
public class LocationEntityTypeConfiguration : IEntityTypeConfiguration<Location>
{
    public void Configure(EntityTypeBuilder<Location> builder)
    {
        builder.HasKey(l => l.Id);
        builder.HasIndex(l => l.LocationId).IsUnique();
        builder.Property(l => l.LocationId).IsRequired();

        builder.HasMany(i => i.ItemLots).WithOne(l => l.Location).HasForeignKey(l => l.LocationId);
    }   
}

bạn dùng ef core version mấy, modify mấy code trên cho dễ nhìn tí.

tui dùng EF core ver 7 á.

As requested

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?