本文介绍了如何首先在实体框架代码中指定复合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在Northwind数据库中设置[Order Details][Products][Orders]之间的映射,我有一个快速的问题.

I have a quick question about setting up the mappings between [Order Details], [Products] and [Orders] in the Northwind datbase.

[Order Details]没有主键,看起来像这样

[Order Details] has no primary key, and looks like this

[Order Details]
OrderId (int)
ProductId (int)
...

所以我的问题是如何(也可以)设置我的OrderDetails类以使其工作?

So my question is how do I (and can I) set up my OrderDetails class to work like this?

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(因为我不知道自己在做什么,所以为空)

And My OrderDetailsConfiguration (empty because I don't know what I'm doing)

public class OrderDetailsConfiguration : EntityConfiguration<OrderDetails>
{
    public OrderDetailsConfiguration()
    {
        //HasKey(x => x.Order.OrderId);
        //HasKey(x => x.Product.ProductId);
    }
}

任何提示或想法都很好.

Any hints or ideas would be great.

推荐答案

首先,您必须在 OrderDetails 类中明确地将PK和FK放入:

First you would have to explicitly put in the PKs and FKs inside OrderDetails class:

public class OrderDetails
{
    public int OrderID { get; set; }
    public int ProductID { get; set; }

    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; }
}

然后使用以下代码,您可以为 OrderDetailsConfiguration 类指定PK:
(请注意,在 Northwind 数据库中, OrderID ProductID 都是 OrderDetails 表的PK.)

Then with the following code you can specify the PKs for OrderDetailsConfiguration class:
(Please note that in Northwind database both OrderID and ProductID are PKs for OrderDetails table).

public class OrderDetailsConfiguration : EntityConfiguration<OrderDetails>
{
    public OrderDetailsConfiguration()
    {
        HasKey(od => new
        {
            od.ProductID,
            od.OrderID
        });
    }
}

您还可以使用 RecreateDatabaseIfModelChanges 策略在每次更改类模型时强制重新创建数据库:

And also you can use RecreateDatabaseIfModelChanges strategy to force your database to be recreated every time you change the class model:

Database.SetInitializer<NorthwindDb>(
        new RecreateDatabaseIfModelChanges<NorthwindDb>());

有趣的一点是,一旦进行了这些更改,EF就会自动从 OrderDetails 类推断FK并创建与 Orders Products 根据 代码优先公约:

The interesting point is that once you made these changes EF will automatically infer the FKs from OrderDetails class and create the relationships to Orders and Products table on OrderID and ProductID based on the Conventions for Code First:

这篇关于如何首先在实体框架代码中指定复合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-31 03:55