Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Ef迁移错误_C#_Entity Framework - Fatal编程技术网

C# Ef迁移错误

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

我需要幻化我的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]
    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; }
}