简体   繁体   English

npgsql CLR 枚举类型必须在使用前向 Npgsql 注册

[英]npgsql The CLR enum type must be registered with Npgsql before usage

I am trying to map my enum at my DBContext but it keeps show and error that my enum type is not registered with Npgsql.我正在尝试在我的 DBContext 中映射我的枚举,但它一直显示和错误表明我的枚举类型未在 Npgsql 中注册。 I have already registered at the static method but it shows an error that my enum is not registered by the clr is there anything i missed out?我已经在静态方法中注册了,但它显示了一个错误,我的枚举没有被 clr 注册,我错过了什么吗?

Error错误

---> 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 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; }
        
}

I faced similar proble on EF Core 5.0.我在 EF Core 5.0 上遇到了类似的问题。 ORM uses default name translator (which is PaskalCase to snake_case) to get the name of the postgres enum type. ORM 使用默认名称转换器(从 PaskalCase 到 snake_case)来获取 postgres 枚举类型的名称。 In your example name of the enum is Enumsetone so postgres type should have name enumsetone.在您的示例中,枚举的名称是 Enumsetone,因此 postgres 类型应该具有名称 enumsetone。 You can check the name by invoking您可以通过调用来检查名称

string name = NpgsqlConnection.GlobalTypeMapper.DefaultNameTranslator.TranslateTypeName(typeof(YourEnum).Name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM