I have 3 models and a User class which defines the common properties for customer and seller so I won't attach it (I guess it would be useless info).
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; }
}
And the last is the 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);
}
}
When I use:
dotnet ef migrations add inital
into the terminal I get this:
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'.
I have no idea how to fix it.