簡體   English   中英

npgsql CLR 枚舉類型必須在使用前向 Npgsql 注冊

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

我正在嘗試在我的 DBContext 中映射我的枚舉,但它一直顯示和錯誤表明我的枚舉類型未在 Npgsql 中注冊。 我已經在靜態方法中注冊了,但它顯示了一個錯誤,我的枚舉沒有被 clr 注冊,我錯過了什么嗎?

錯誤

---> System.NotSupportedException: The CLR enum type enumsetone must be registered with Npgsql before usage, please refer to the documentation.

數據庫上下文

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

啟動文件

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
}

響應文件

[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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM