如何首先在实体框架代码中指定复合键 [英] How to Specify a Compound key in Entity Framework Code First

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

问题描述

关于在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:

Code First将推断出名称为<navigation property name><primary key property name>(即ProductsProductID),<principal class name><primary key property name>(即ProductProductID)或<primary key property name>(即ProductID)的任何属性,其数据类型与主键相同,均表示关系.如果找到多个匹配项,则按照上面列出的顺序给出优先级.外键检测将不区分大小写.

Code First will infer that any property named <navigation property name><primary key property name> (i.e. ProductsProductID), <principal class name><primary key property name> (i.e. ProductProductID) or <primary key property name> (i.e. ProductID), with the same data type as the primary key, represents a foreign key for the relationship. If multiple matches are found then precedence is given in the order listed above. Foreign key detection will not be case sensitive.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆