我有一个关于在 Northwind 数据库中设置和之间[Order Details]
的映射的快速问题。[Products]
[Orders]
[Order Details]
没有主键,看起来像这样
[Order Details]
OrderId (int)
ProductId (int)
...
所以我的问题是我如何(并且我可以)设置我的OrderDetails
班级以这样的方式工作?
public class OrderDetails
{
public virtual Order Order { get; set; }
public virtual Product Product { get; set; }
public Decimal UnitPrice { get; set; }
public int Quantity { get; set; }
public Decimal Discount { get; set; }
}
我的数据上下文看起来像这样
public class NorthwindDb : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<OrderDetails> OrderDetails { get; set; }
public DbSet<Customer> Customers { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new OrderDetailsConfiguration());
}
public static void InitializeBecauseOfThatWeirdMetaDataThingThatIDontUnderstandYet()
{
Database.SetInitializer<NorthwindDb>(null);
}
}
还有我的OrderDetailsConfiguration
(空的,因为我不知道我在做什么)
public class OrderDetailsConfiguration : EntityConfiguration<OrderDetails>
{
public OrderDetailsConfiguration()
{
//HasKey(x => x.Order.OrderId);
//HasKey(x => x.Product.ProductId);
}
}
任何提示或想法都会很棒。