开发者

How can I define a key in EF4.1 Code First on a property that cannot be an identity or guid?

开发者 https://www.devze.com 2023-02-25 06:59 出处:网络
I have a simple class: public class Member { public int AccountId { get; set; } public string Name { get; set; }

I have a simple class:

public class Member
{
    public int AccountId { get; set; }
    public string Name { get; set; }
    public string Role { get; set; }
    public DateTime MemberSince { 开发者_开发百科get; set; }
}

I need to make the AccountId a key but if I use the "Key" attribute it turns it into an identity which won't work for me (this data comes from another source so the value cannot be changed).

Is there another way to do this?

There is almost the exact same question here except the answer uses the fluent interface which I cannot do.


Sure, you can use:

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

精彩评论

暂无评论...
验证码 换一张
取 消