如何使用实体框架迁移为自定义类型添加新值?
我假设一个名为用户
的实体,带有枚举类型属性user_type
带有以下值:admin> admin
and editor
。
我想为此枚举添加一个新值:贡献者
,但是我无法使实体框架生成脚本以更新具有新值的枚举类型。
我有:
// The Enum itself
public enum UserType : byte
{
admin,
editor,
contributor
}
//Entity class
public class User
{
[Column("user_type")]
[Required]
public UserType UserType { get; set; }
}
// My DbContext class
static MyDbContext()
{
NpgsqlConnection.GlobalTypeMapper.MapEnum<UserType>("user_type");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ForNpgsqlHasEnum("user_type", new[] { "admin", "editor", "contributor" });
modelBuilder.Entity<UserType>();
}
当我运行命令时:
dotnet ef migrations script --idempotent PREVIOUS_MIGRATION_NAME CURRENT_MIGRATION_NAME --output CURRENT_MIGRATION_NAME.sql
我看不到更改新值的类型。我本来会期望的类似:
ALTER TYPE user_type ADD VALUE 'contributor'
我正在使用.NET Core 3.1和实体框架Core .NET命令行工具6.0.5
I have let's say an entity named User
with an Enum type property named user_type
with the following values: admin
and editor
.
I want to add a new value to this Enum: contributor
, but I'm not able to make Entity Framework generate the script to update the Enum type with the new value.
I have:
// The Enum itself
public enum UserType : byte
{
admin,
editor,
contributor
}
//Entity class
public class User
{
[Column("user_type")]
[Required]
public UserType UserType { get; set; }
}
// My DbContext class
static MyDbContext()
{
NpgsqlConnection.GlobalTypeMapper.MapEnum<UserType>("user_type");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ForNpgsqlHasEnum("user_type", new[] { "admin", "editor", "contributor" });
modelBuilder.Entity<UserType>();
}
When I run the command:
dotnet ef migrations script --idempotent PREVIOUS_MIGRATION_NAME CURRENT_MIGRATION_NAME --output CURRENT_MIGRATION_NAME.sql
I don't see anything about altering the type with the new value. I would have expected something like:
ALTER TYPE user_type ADD VALUE 'contributor'
I'm using .NET Core 3.1 and Entity Framework Core .NET Command-line Tools 6.0.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论