无法确定类型 'ICollection<Product>' 的导航 'Customer.BoughtProducts' 表示的关系

Unable to determine the relationship represented by navigation 'Customer.BoughtProducts' of type 'ICollection<Product>'

我有 3 个模型和一个用户 class,它定义了客户和卖家的共同属性,所以我不会附加它(我想这将是无用的信息)。

public class Seller : User
{
    public bool IsApproved { get; set; }

    public ICollection<Product> WaitingForShipping { get; set; }

    public ICollection<Product> ProductsOnSell { get; set; }
} 

public class Customer : User
{
    public bool IsSubscriber { get; set; }

    public ICollection<Product> WaitingForReceiving { get; set; }

    public ICollection<Product> BoughtProducts { get; set; }
}

public class Product
{
    [Key] public Guid Id { get; set; }

    public string Name { get; set; }

    public decimal Price { get; set; }

    public string Description { get; set; }

    public ProductCategory Category { get; set; }

    public int SellerId { get; set; }
    [ForeignKey("SellerId")] public Seller ProductSeller { get; set; }

    public int CustomerId { get; set; }

    [ForeignKey("CustomerId")] public Customer ProductCustomer { get; set; }

    public ICollection<Characteristic> ProductInfo { get; set; }

    public ICollection<Image> ImagesURLs { get; set; }

    public long Quantity { get; set; }

    public long AmountSold { get; set; }

    public byte Rating { get; set; }

    public bool InStock { get; set; }
}

最后一个是 DbContext class

public class AppDbContext : DbContext
{
    public DbSet<Customer> Customers { get; set; }

    public DbSet<Seller> Sellers { get; set; }

    public DbSet<Product> Products { get; set; }

    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        
        // There were a lot of useless tries of configuring the relationships like
        // this
        // modelBuilder.Entity<Customer>().HasMany(prods => prods.BoughtProducts)
        // .WithOne(s => s.ProductSeller).HasForeignKey(i => i.SellerId);
        base.OnModelCreating(modelBuilder);
    }
}

当我使用:

dotnet ef migrations add inital

进入终端我得到这个:

Unable to determine the relationship represented by navigation'Customer.BoughtProducts' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

我不知道如何解决它。

我不知道您的客户代码。但我猜你可能使用 ICollection<Product> 不止一个。以卖家为例

根据您的Seller.cs,我在产品中添加了以下代码,

        public Guid SellerId { get; set; }
        public Seller ProductSeller { get; set; }

        public Guid OnSellId { get; set; }
        public Seller OnSell { get; set; }

并在上下文中添加.OnDelete(DeleteBehavior.Restrict);

 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Product>()
       .HasOne(p => p.ProductSeller)
       .WithMany(t => t.WaitingForShipping)
       .HasForeignKey(m => m.SellerId)
       .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<Product>()
                .HasOne(p => p.OnSell)
                .WithMany(t => t.ProductsOnSell)
                .HasForeignKey(m => m.OnSellId)
                .OnDelete(DeleteBehavior.Restrict);


        }

有效,我的 customer.cs 是 below.If 你的 Customer.cs 像卖家一样,像我一样更改产品和上下文中的代码。

public class Customer
    {
        public int CustomerId { get; set; }
        public string CustomerName { get; set; }
      
       
        public ICollection<Product> BoughtProducts { get; set; }
       
    }

客户更新

在上下文中添加:

modelBuilder.Entity<Product>()
       .HasOne(p => p.ProductCustomer)
       .WithMany(t => t.WaitingForReceiving)
       .HasForeignKey(m => m.CustomerId)
       .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<Product>()
                .HasOne(p => p.BoughtCustomer)
                .WithMany(t => t.BoughtProducts)
                .HasForeignKey(m => m.BoughtId)
                .OnDelete(DeleteBehavior.Restrict);

添加产品:

       public Guid CustomerId { get; set; }
       public Customer ProductCustomer { get; set; }
        
        public Guid BoughtId { get; set; }
        public Customer BoughtCustomer { get; set; }