如何解决使用前必须在Npgsql中注册CLR枚举类型
我试图在我的DBContext上映射我的枚举,但是它一直显示和错误,我的枚举类型未在Npgsql中注册。我已经在静态方法中注册了,但显示错误,我的枚举未由clr注册,是否有我错过的东西?
错误
---> System.NotSupportedException: The CLR enum type enumsetone must be registered with Npgsql before usage,please refer to the documentation.
DBContext.cs
public virtual DbSet<Response> response { get; set; }
static void RegisterTypes()
{
NpgsqlConnection.GlobalTypeMapper.MapEnum<enumsetone>();
NpgsqlConnection.GlobalTypeMapper.MapEnum<enumsettwo>();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("public");
modelBuilder.HasPostgresEnum<enumsetone>();
modelBuilder.HasPostgresEnum<enumsettwo>();
}
Startup.cs
public virtual void ConfigureServices(IServiceCollection services)
{
DBContext.RegisterTypes();
services.AddDbContextPool<DBContext>(options => options.UseNpgsql(_configuration.GetConnectionString("PostgresSqlDb")));
}
Enumsetone.cs
[JsonConverter(typeof(StringEnumConverter))]
public enum Enumsetone
{
GET,POST
}
Response.cs
[Table("response",Schema = "public")]
public class Response
{
[Key]
[Column("id")]
public Guid ResponseId { get; set; }
[Column("source")]
public enumsetone Source { get; set; }
[Column("destination")]
public enumsetone Destination { get; set; }
[Column("status_code")]
public enumsettwo EnumSetTwo { get; set; }
}
解决方法
我在 EF Core 5.0 上遇到了类似的问题。 ORM 使用默认名称转换器(从 PaskalCase 到 snake_case)来获取 postgres 枚举类型的名称。在您的示例中,枚举的名称是 Enumsetone,因此 postgres 类型应该具有名称 enumsetone。您可以通过调用
来检查名称string name = NpgsqlConnection.GlobalTypeMapper.DefaultNameTranslator.TranslateTypeName(typeof(YourEnum).Name);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。