实体框架没有定义键:键注释

时间:2013-04-26 08:46:27

标签: c# entity-framework

我使用了Users实体类:

 public class Users
{
    public int UserId { get; set; } 
    public string NickName { get; set; }
    public string FirstName { get; set; }
    public string Password { get; set; }
    public DateTime BirthDate { get; set; }
    public int Age { get; set; }
    public string CountryName { get; set; }
    public string Email { get; set; }
    public int Gender { get; set; }
    public int DayInvitation { get; set; }
    public string CityName { get; set; }
    public string EyesColors { get; set; }
    public string Avatar { get; set; }
    public int Popularity { get; set; }
} 

我收到以下错误:

  

System.Data.Entity.Edm.EdmEntityType :: EntityType'Users'没有定义键。

所以我添加了[Key]关键字..

 public class Users
{
    [Key]
    public int UserId { get; set; } 
    public string NickName { get; set; }
    public string FirstName { get; set; }
    // ... 
} 

现在我的问题是,我很确定我做了另一个ASP .NET PROJECT(我的第一个,这是第二个)而不使用这个[Key]注释,只需指定ProductId,有人可以解释我吗为什么?我无法理解..

1 个答案:

答案 0 :(得分:3)

自动主键名称发现约定如下:

  • 您有一个名为Id
  • 的媒体资源
  • 或者您有一个名为YourClassNameId的商品。

在您的情况下,您的班级名为Users,最后带有“s”。

因此,为了参加会议工作,您需要为您的财产命名UsersId(请注意“s”):

public class Users
{
    public int UsersId { get; set; } 
    //...
}

或仅Id

public class Users
{
    public int Id { get; set; } 
    //...
}

如果您想了解更多关于对流的信息,可以从这篇精彩的文章开始:

Code First Conventions

可以在Entity Framework Learning center.

找到
相关问题