EF Core with NodaTime and Hangfire

217 Views Asked by At

I'm setting up a new project and I'm using EF Core for persistence with a Postgres database. I added NodaTime as a dependency, because I want to use the Period type.

After setting up the DbContext with the extension method UseNodaTime(), everything worked fine.

Configuraton of DbContext:

services.AddDbContext<MyDbContext>(options => {
             options.UseNpgsql(connectionString, postgresOptions => {
                     postgresOptions.UseNodaTime();
             })
            .UseSnakeCaseNamingConvention();         
         }); 

Later I added Hangfire for some background task processing and scheduled jobs. To keep it simple, i wanted to use the same database instance for Hangfire.

Configuration of Hangfire:

services.AddHangfire(options => {
             options.UsePostgreSqlStorage(connectionString);
}); 
services.AddHangfireServer(new PostgreSqlStorage(connectionString);

After the configuration of Hangfire, the existing query stopped working with the following exception.

fail: Microsoft.EntityFrameworkCore.Query[10100]

An exception occurred while iterating over the results of a query for context type 'Subscriptions.Infrastructure.Persistence.SubscriptionContext'.

System.InvalidCastException: Can't cast database type interval to Period

at Npgsql.Internal.TypeHandling.NpgsqlTypeHandler.ReadCustom[TAny](NpgsqlReadBuffer buf, Int32 len, Boolean async, FieldDescription fieldDescription)
at Npgsql.NpgsqlDataReader.GetFieldValue[T](Int32 ordinal)
at lambda_method31(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.AsyncEnumerator.MoveNextAsync() System.InvalidCastException: Can't cast database type interval to Period at Npgsql.Internal.TypeHandling.NpgsqlTypeHandler.ReadCustom[TAny](NpgsqlReadBuffer buf, Int32 len, Boolean async, FieldDescription fieldDescription) at Npgsql.NpgsqlDataReader.GetFieldValue[T](Int32 ordinal) at lambda_method31(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.AsyncEnumerator.MoveNextAsync()

I did some research, but I'm currently not getting any closer to the solution. It seems to me that Dapper is overwriting or removing the registered type converters for NodaTime.

Can anyone help me with this? Thank you very much for your help!

I have tried to add a type converter for dapper described here.

0

There are 0 best solutions below