Failed to save string value to PostgreSQL enum column

Aperta
#3,529 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
csharp, postgresql
Ambito
databases

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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?

Lingua principale
C#
Stelle
1.8k
Fork
268
Merge medio
2h 35m
PR unite (30g)
136

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di npgsql/efcore.pg

Tutte le issue di npgsql/efcore.pg

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.