我收到以下错误:
One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'UserAccount' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'UserAccounts' is based on type 'UserAccount' that has no keys defined.
此错误由代码触发:
_Db.Database.Initialize(true);
我认为由于某种原因它没有拾取模型上的 [Key] 属性。最初当我尝试运行我没有添加 key 属性的代码时,这是否意味着已经创建/缓存了阻止应用此键的内容?
除了包含 Entity Framework 5 之外,MVC4 项目几乎是一个空白设置
型号代码
public class AccountContext: DbContext
{
public AccountContext()
: base("DefaultConnection")
{
}
public DbSet<UserAccount> UserAccounts { get; set; }
}
[Table("UserAccount")]
public class UserAccount
{
[Key]
[Required]
public string Username;
[Required]
[DataType(DataType.Password)]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
public string Password;
public string Name;
public string Surname;
[Required]
public string Email;
}
全局.asax 初始化
Database.SetInitializer(new DropCreateDatabaseAlways<AccountContext>());
var _Db = new AccountContext();
_Db.Database.Initialize(true);
我已经进行了一些搜索并了解了命名约定,例如 Id / UserId 等。但是我想明确使用 [Key] 并调用字段 Username。