2014-08-27 110 views
0

我需要mirate我的DbContext类,但我得到这个错误:Ef中迁移错误

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

默认情况下,实体框架仅查看公共属性。你的物业不公开,所以他们被忽略。 解决这个问题的最简单方法是让他们公开:

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

迁移然后将正常工作。

+0

谢谢,它的工作 – 2014-08-28 06:34:55