如何使用实体框架迁移为自定义类型添加新值?

发布于 2025-02-05 15:16:57 字数 1187 浏览 2 评论 0原文

我假设一个名为用户的实体,带有枚举类型属性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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文