在实体框架代码中,首先如何在多个列上使用KeyAttribute [英] in entity framework code first, how to use KeyAttribute on multiple columns

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

问题描述

我正在创建一个POCO模型,使用实体框架代码第一个CTP5。我正在使用装饰使属性映射到PK列。但是如何在一列上定义一个PK,具体来说,如何控制索引中的列的顺序?是否是类中属性顺序的结果?

I'm creating a POCO model to use with entity framework code first CTP5. I'm using the decoration to make a property map to a PK column. But how can I define a PK on more then one column, and specifically, how can I control order of the columns in the index? Is it a result of the order of properties in the class?

谢谢!

推荐答案

您可以在属性中指定列顺序,例如:

You can specify the column order in the attributes, for instance:

public class MyEntity
{
    [Key, Column(Order=0)]
    public int MyFirstKeyProperty { get; set; }

    [Key, Column(Order=1)]
    public int MySecondKeyProperty { get; set; }

    [Key, Column(Order=2)]
    public string MyThirdKeyProperty { get; set; }

    // other properties
}

如果你是使用 DbSet Find 方法,您必须考虑这个关键参数的这个订单。

If you are using the Find method of a DbSet you must take this order for the key parameters into account.

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

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