如何在不能成为身份或guid的属性上定义EF4.1 Code First中的密钥?

时间:2011-04-15 14:08:49

标签: entity-framework mapping data-annotations entity-framework-4.1

我有一个简单的课程:

public class Member
{
    public int AccountId { get; set; }
    public string Name { get; set; }
    public string Role { get; set; }
    public DateTime MemberSince { get; set; }
}

我需要将AccountId作为密钥,但如果我使用“密钥”属性,则将其转换为对我不起作用的标识(此数据来自其他来源,因此无法更改该值)。

还有其他办法吗?

除了答案使用了我不能做的流畅界面之外,还有almost the exact same question here

1 个答案:

答案 0 :(得分:1)

当然,您可以使用:

[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int AccoungId { get; set; }