Failed to save string value to PostgreSQL enum column

オープン
#3,529 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
csharp, postgresql
領域
databases

調査の方向性

Start by checking how EF Core value conversions interact with Npgsql's PostgreSQL enum mapping; the report provides a converter, model configuration, SQL schema, and exception, but names no repository files or tests. Reproduce the save failure with the provided setup, then look for relevant provider tests or documentation. Done means establishing whether this configuration can save the value and documenting or fixing the behavior.

索引モデルが issue の本文から書いたものです。

説明

I have C# value object together with convert like this:

public record DomainType
{
    public string Value { get; }

    public static readonly DomainType DomainTypeValue1 = new("DomainTypeValue1");
    public static readonly DomainType DomainTypeValue2 = new("DomainTypeValue2");
    public static readonly DomainType DomainTypeValue3 = new("DomainTypeValue3");

    private DomainType(string value)
    {
        Value = value;
    }

    public static DomainType From(string value)
    {
        return new DomainType(value);       
    }
    public override string ToString()
    {
        return Value;
    }
}

public class DomainTypeConverter() : ValueConverter<DomainType, string>(v => v.Value,  v => DomainType.From(v));

I use this type in the entity:

[EntityTypeConfiguration<DomainEntityModelBuilder, DomainEntity>]
[Table("domain_entity", Schema = "my_schema")]
public class DomainEntity
{
    public required Guid Id { get; init; }  

    public required DomainType EntityType { get; init; }
}

Model builder looks like this:

public class DomainEntityModelBuilder : IEntityTypeConfiguration<DomainEntity>
{
    public void Configure(EntityTypeBuilder<DomainEntity> entity)
    {
        entity.Property(d => d.Id).ValueGeneratedNever();

        entity
            .Property(d => d.EntityType )
            .HasColumnName("entity_type")
            .HasConversion<DomainTypeConverter>()
            .HasColumnType("my_schema.domain_type");
    }
}

Sql to create enum and type looks like this:

CREATE TYPE my_schema.domain_type AS ENUM ('DomainTypeValue1', 'DomainTypeValue2', 'DomainTypeValue3');

CREATE TABLE my_schema.domain_entity
(
    id                           UUID PRIMARY KEY NOT NULL,    
    entity_type            my_schema.domain_type     NOT NULL
)

When I tried to save DomainEntity I get error:

Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
---> Npgsql.PostgresException (0x80004005): 42804: column "entity_type" is of type my_schema.domain_type but expression is of type text

Is it possible to save text value as a postgres enum type?

主要言語
C#
スター
1.8k
フォーク
268
平均マージ
2時間 35分
マージ済み PR(30日)
136

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

npgsql/efcore.pg のほかの issue

npgsql/efcore.pg の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。