One or more validation errors were detected during model generation:
System.Data.Edm.EdmEntityType: : EntityType 'UserInfo' has no key defined. Define the key for this EntityType. System.Data.Edm.EdmEntitySet: EntityType: EntitySet �UserInfo� is based on type �UserInfo� that has no keys defined.
遇見這個問題,我覺得很奇特,因為事實上我已經為'UserInfo'這個類定義了[KEY]的類注釋。
然后又提示我找不到Key。見下面的代碼
1 public class UserInfo 2 { 3 [Key] 4 public int UserID; 5 public string UserName; 6 public string Password; 7 public int UseState; 8 public string Email; 9 public DateTime AddTime; 10 public int AddUser_ID; 11 public string ImgUrl; 12 public virtual UserType UserTypes { get; set; } 13 }
后面在stackoverflow上找到了答案。EF會自動識別一個實體的主鍵只要主鍵的名稱符號 'Id'或 '實體名Id'. 另外,它必須聲明成屬性,訪問權限必須是Public的。這個錯誤是因為我將UserId聲明成了一個字段,只要修改成屬性就OK了。修改后的代碼如下所示。
1 public class UserInfo 2 { 3 [Key] 4 public int UserID { get; set; } 5 public string UserName { get; set; } 6 public string Password { get; set; } 7 public int UseState { get; set; } 8 public string Email { get; set; } 9 public DateTime AddTime { get; set; } 10 public int AddUser_ID { get; set; } 11 public string ImgUrl{ get; set; } 12 public virtual UserType UserTypes { get; set; } 13 }
2013-01-10 16:50:05