C# 使用枚举获取实体框架迁移错误
我有一个实体和一个枚举,如下所示:C# 使用枚举获取实体框架迁移错误,c#,.net,entity-framework,entity-framework-core,entity-framework-migrations,C#,.net,Entity Framework,Entity Framework Core,Entity Framework Migrations,我有一个实体和一个枚举,如下所示: public class RequestType { public Guid RequestTypeId { get; set; } public RequestActionEnum Name { get; set; } public ICollection<Request> Requests { get; set; } } 我正在使用以下代码使用Entity Framework Core进行数据库迁移: protect
public class RequestType
{
public Guid RequestTypeId { get; set; }
public RequestActionEnum Name { get; set; }
public ICollection<Request> Requests { get; set; }
}
我正在使用以下代码使用Entity Framework Core进行数据库迁移:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<RequestType>().HasData(
new RequestType { RequestTypeId = Guid.NewGuid(), Name = RequestActionEnum.New },
new RequestType { RequestTypeId = Guid.NewGuid(), Name = RequestActionEnum.Update },
new RequestType { RequestTypeId = Guid.NewGuid(), Name = RequestActionEnum.Archive }
);
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity().HasData(
新请求类型{RequestTypeId=Guid.NewGuid(),Name=RequestActionEnum.new},
新请求类型{RequestTypeId=Guid.NewGuid(),Name=RequestActionEnum.Update},
新请求类型{RequestTypeId=Guid.NewGuid(),Name=RequestActionEnum.Archive}
);
}
但是,当使用更新数据库
更新数据库时,出现以下错误:
列“Name”不能自动转换为整型
我使用的是实体框架核心3.0和代码优先的方法,我不知道为什么会出现这个错误
有人能就这个问题提出一些建议吗
非常感谢。众所周知,EntityFramework不能很好地处理枚举值。我不知道这是否首先适用于代码,但我已经成功地使用类似的方法将列值映射到枚举。在您的实体(POCO)中:
这不是最优雅的解决方案,但它可能会起作用。我倾向于使用。是否需要明确指定枚举存储为数字?数据库中
Name
列的数据类型是什么?我使用的是枚举创建数据库和代码优先方法
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<RequestType>().HasData(
new RequestType { RequestTypeId = Guid.NewGuid(), Name = RequestActionEnum.New },
new RequestType { RequestTypeId = Guid.NewGuid(), Name = RequestActionEnum.Update },
new RequestType { RequestTypeId = Guid.NewGuid(), Name = RequestActionEnum.Archive }
);
}
[NotMapped]
public RequestActionEnum Name
{
get { return (RequestActionEnum)NameValue; }
set { NameValue = (int)value; }
}
[Column("Name")]
public int NameValue { get; set; }