C# Ef迁移错误
我需要幻化我的dbContext类,但我遇到了以下错误:C# Ef迁移错误,c#,entity-framework,C#,Entity Framework,我需要幻化我的dbContext类,但我遇到了以下错误: System.Data.Entity.Edm.EdmEntityType: : EntityType 'UserType' has no key defined. Define the key for this EntityType. System.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'UserTypes' is based on type 'UserType' that
System.Data.Entity.Edm.EdmEntityType: : EntityType 'UserType' has no key defined. Define the key for this EntityType.
System.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'UserTypes' is based on type 'UserType' that has no keys defined.
以下是我的DbContext:
namespace seraydar.Models
{
public class Context : DbContext
{
public DbSet<UserType> UserTypes { get; set; }
}
}
我只是糊涂了 默认情况下,实体框架只查看公共属性。您的属性不是公共的,因此将忽略它们。 解决此问题的最简单方法是将其公开:
public class UserType
{
[Key]
public int id { get; set; }
[Required, MaxLength(30, ErrorMessage = "Name can not be longer than 30 characters."), Display(Name = "User Type")]
public string userType { get; set; }
}
然后迁移将正常工作
public class UserType
{
[Key]
public int id { get; set; }
[Required, MaxLength(30, ErrorMessage = "Name can not be longer than 30 characters."), Display(Name = "User Type")]
public string userType { get; set; }
}