Ef迁移错误

时间:2014-08-27 23:01:48

标签: 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 has no keys defined.

这是我的DbContext:

namespace seraydar.Models
{
    public class Context : DbContext
    {
        public DbSet<UserType> UserTypes { get; set; }
    }
}

这是我的班级:

public class UserType
    {
        [Key]
        int id { get; set; }

        [Required, MaxLength(30, ErrorMessage = "Name can not be longer than 30 characters."), Display(Name = "User Type")]
        string userType { get; set; }
    }

我很困惑!

1 个答案:

答案 0 :(得分:1)

默认情况下,Entity Framework仅查看公共属性。您的属性不公开,因此会被忽略。 解决这个问题的最简单方法是公开它们:

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; }
}

然后,迁移将正常运行。