Failed to save string value to PostgreSQL enum column

Aberta
#3,529 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
25/100
Tipo de issue
Bug
Clareza
Razoavelmente clara
Status de atividade
Estagnada
Stack de tecnologia
csharp, postgresql
Domínio
databases

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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?

Linguagem predominante
C#
Estrelas
1.8k
Forks
268
Merge médio
2h 35min
PRs com merge (30d)
136

Preparar o ambiente

Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de npgsql/efcore.pg

Todas as issues de npgsql/efcore.pg

Issues semelhantes

Mais issues de C#

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.