Entity framework core not able to process enums when using postgresql provider

0

I have a weird issue with postgress provider of entity framework core. One of my entities has an enum property type.

The entity is configured as below:

modelBuilder.Entity<PropertyDbEntity>(entity =>
{
    entity.ToTable("properties");

   
    entity.Property(x => x.PropertyType).HasColumnName("property_type");

    ...
});

I have added necessary configs for the enum to work as per documentation.

public class MyDbContext : Microsoft.EntityFrameworkCore.DbContext
{
    static MyDbContext()
    {
        NpgsqlConnection.GlobalTypeMapper.MapEnum<PropertyType>();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasPostgresEnum<PropertyType>();
      
        //....
    }
}

But whenever I try to add one entity of this type I get exception as below:

Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> Npgsql.PostgresException (0x80004005): 42804: column "property_type" is of type property_type but expression is of type integer at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<g__ReadMessageLong|0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<g__ReadMessageLong|0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming) at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken) Exception data: Severity: ERROR SqlState: 42804 MessageText: column "property_type" is of type property_type but expression is of type integer Hint: You will need to rewrite or cast the expression. Position: 94 File: d:\pginstaller_12.auto\postgres.windows-x64\src\backend\parser\parse_target.c Line: 592 Routine: transformAssignedExpr --- End of inner exception stack trace --- at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList1 entriesToSave, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(DbContext _, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken) at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 operation, Func`4 verifySucceeded, CancellationTo ken cancellationToken) at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)

I have run all migrations and the enum exists in db, also the values of that type in db are the same ones enum has in c#.

Does anybody understand what might be the issue ?

My Asp.Net core version is 3.1 and the entity framework core versions is: 3.1.8, while Npgsql.EntityFrameworkCore.PostgreSQL package version is 3.1.4

c#
entity-framework-core
npgsql
asked on Stack Overflow Sep 23, 2020 by Rajmond Burgaj • edited Sep 23, 2020 by Camilo Terevinto

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0