linq2db.EntityFrameworkCore: EF Core + Npgsql + NodaTime + linq2db - Cannot convert to DateTime

I am having a weird bug when using Npgsql’s NodaTime bindings. Each non-NodaTime binding just breaks with the following exceptions:

Cannot convert value '2021-09-16T18:14:14Z' to type 'System.DateTime': Object must implement IConvertible.
   at LinqToDB.Common.ConvertBuilder.ConvertDefault(Object value, Type conversionType)
   at LinqToDB.Expressions.ConvertFromDataReaderExpression.ColumnReader.GetValue(IDataReader dataReader)
   at LinqToDB.Linq.QueryRunner.Mapper`1.Map(IDataContext context, IQueryRunner queryRunner, IDataReader dataReader)
   at LinqToDB.Linq.QueryRunner.AsyncEnumeratorImpl`1.<MoveNextAsync>d__18.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.<ToListAsync>d__65`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.<ToListAsync>d__65`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.<ToArrayAsync>d__66`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at LinqToDB.AsyncExtensions.<ToArrayAsync>d__9`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

The same query when run through EF Core works just fine, but breaks through Linq2Db.

I’ve set up a simple repro:

One-file repro
using LinqToDB.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;

namespace L2DbBugRepro
{
    class Program
    {
        static async Task Main(string[] args)
        {
            await using (var ctx = new BlogContext())
            {
                await ctx.Database.EnsureDeletedAsync();
                await ctx.Database.EnsureCreatedAsync();

                ctx.Facts.Add(new Fact
                {
                    FactId = Guid.NewGuid(),
                    Date = DateTime.Now,
                });
                await ctx.SaveChangesAsync();
            }

            await using (var ctx = new BlogContext())
            {
                var facts = await ctx.Facts.ToArrayAsyncLinqToDB();
            }


        }
    }


    public class BlogContext : DbContext
    {
        public DbSet<Fact> Facts { get; set; }

        static ILoggerFactory ContextLoggerFactory
            => LoggerFactory.Create(b => b.AddConsole().AddFilter("", LogLevel.Information));

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseNpgsql(@"Host=localhost;Username=root;Password=rootpass;Database=Testing", n => n.UseNodaTime())
    .EnableSensitiveDataLogging()
                .UseLoggerFactory(ContextLoggerFactory);

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
        }
    }

    public class Fact
    {
        [Key]
        public Guid FactId { get; set; }

        public DateTime Date { get; set; }
        // Other fields redacted
    }
}

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 25 (13 by maintainers)

Most upvoted comments

Check latest version that have released today. It supports EF Core 6.0.0.

Probably you forgot to call ToLinqToDB(). Because it is EF Core exception.

I even don’t think that NodaTime.Instant -> DataParameter conversion is needed.

Reproduced. Trying to figure out what happened.