【问题标题】:How can I define a key in EF4.1 Code First on a property that cannot be an identity or guid?如何在 EF4.1 Code First 中对不能作为标识或 guid 的属性定义键?
【发布时间】:2011-04-15 14:08:48
【问题描述】:

我有一个简单的课程:

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

我需要将 AccountId 设为键,但如果我使用“Key”属性,它会将其转换为对我不起作用的身份(此数据来自其他来源,因此无法更改值)。

还有其他方法吗?

almost the exact same question here 除了答案使用我做不到的流畅界面。

【问题讨论】:

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


    【解决方案1】:

    当然,你可以使用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 2013-10-21
      • 1970-01-01
      • 2019-05-03
      • 1970-01-01
      相关资源
      最近更新 更多