I create a webshop with .Net Core and I'm using Entity Framework code-first migration.
I try to implement a product-order, many-to-many connection and I got the following error:
Unable to determine the relationship represented by navigation property 'Order.Products' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'
My implementations are:
Order.cs
public class Order
{
[Key]
public int OrderId { get; set; }
...
public virtual ICollection<Product> Products { get; set; }
}
Product.cs
public class Product
{
[Key]
public int ProductId { get; set; }
...
public virtual ICollection<Order> Orders { get; set; }
}
OrderProducts.cs
public class OrderProduct
{
[Key]
public int Id { get; set; }
[ForeignKey("Order")]
public int OrderId { get; set; }
[ForeignKey("Product")]
public int ProductId { get; set; }
public virtual Order Order { get; set; }
public virtual Product Product { get; set; }
}
MyContext:
public class WebShopContext : IdentityDbContext
{
public WebShopContext(DbContextOptions<WebShopContext> options)
: base(options)
{
}
public DbSet<Product> Product { get; set; }
public DbSet<Order> Order { get; set; }
public DbSet<OrderProduct> OrderProduct { get; set; }
}
What am I doing wrong?