Skip to content

Cannot mix DateTimeOffset with Nodatime #2295

Closed

Description

@EricStG

Steps to reproduce

using (var conn = new NpgsqlConnection(ConnectionString))
{
    conn.Open();
    conn.TypeMapper.UseNodaTime();
    using (var cmd = conn.CreateCommand())
    {
        cmd.CommandText = "SELECT @a";
        cmd.Parameters.AddWithValue("a", DateTimeOffset.Now);
        var r = cmd.ExecuteScalar();
        Console.WriteLine(r);
    }
}

The issue

When enabling Nodatime mapping (either via the global type mapper or the per connection mapper), we lose the ability to map System.DateTime and System.DateTimeOffset. Not sure if that's by design or not.

Having a hard time working around this problem since Npgsql.EntityFrameworkCore.PostgreSQL sets this using the global type mapper, preventing us from enabling Nodatime only on certain connections, and we can't really make things uniform because of dependencies on 3rd party libraries.

Exception message:

System.NotSupportedException: The CLR type System.DateTimeOffset isn't natively supported by Npgsql or your PostgreSQL. To use it with a PostgreSQL composite you need to specify DataTypeName or to map it, please refer to the documentation.

Stack trace:

   at Npgsql.TypeMapping.ConnectorTypeMapper.GetByClrType(Type type) in C:\projects\npgsql\src\Npgsql\TypeMapping\ConnectorTypeMapper.cs:line 144
   at Npgsql.NpgsqlParameter.ResolveHandler(ConnectorTypeMapper typeMapper) in C:\projects\npgsql\src\Npgsql\NpgsqlParameter.cs:line 529
   at Npgsql.NpgsqlCommand.ValidateParameters() in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 796
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1140
   at Npgsql.NpgsqlCommand.ExecuteScalar(Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1084
   at Npgsql.NpgsqlCommand.ExecuteScalar() in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1061
   at TestNPgNoda.Program.Main(String[] args) in C:\Users\echal\source\repos\TestNPgNoda\TestNPgNoda\Program.cs:line 23

Further technical details

Npgsql version: 4.0.4
PostgreSQL version: 11
Operating system: Windows 10 Pro

Other details about my project setup:

Activity

  1. austindrenski commented on Jan 17, 2019

    @austindrenski
    Contributor

    The problem you are encountering with EF Core is a known limitation of the plugin model. Currently, adding the NodaTime plugin is an all-in change, meaning you must use either all BCL or all NodaTime types.

    There is some ongoing work to improve the performance of per-connection plugins (i.e. avoid the global mapper), but that work is planned for EF Core 3.0.

    I'm going to close this for now, but feel free to continue posting if you have further questions.

    See: npgsql/efcore.pg#747

  2. roji commented on Jan 17, 2019

    @roji
    Member

    @austindrenski it doesn't seem like @EricStG is trying to use EF Core, just Npgsql, so I think he's complaining that setting up the NodaTime plugin removes the BCL date/time type mappings... He'd like to be able to use both NodaTime and DateTime with the same Npgsql ADO.NET connection.

    I don't think this is covered by any of the existing issues (although I may be confused with all the discussions we've had recently around this stuff), so it may be worth reopening. I'm still not convinced we want to change the type mapping model to allow this, but it would at least give users an opportunity to upvote it etc.

  3. austindrenski commented on Jan 17, 2019

    @austindrenski
    Contributor

    @roji I was responding to this comment, which I took to mean that this was just an unwrapped version of a higher level issue @EricStG was experiencing:

    Having a hard time working around this problem since Npgsql.EntityFrameworkCore.PostgreSQL sets this using the global type mapper, preventing us from enabling Nodatime only on certain connections, and we can't really make things uniform because of dependencies on 3rd party libraries.

    If I've misunderstood, then this should definitely be reopened for discussion.

  4. roji commented on Jan 17, 2019

    @roji
    Member

    Ah sorry, it is I who read too quickly - I guess I just looked at the repro code. I do remember at least one other user asking about using both NodaTime and regular BCL date/time types on the same Npgsql connection (regardless of EF Core). I honestly think it's a pretty exotic requirement which would also require some significant rethinking of how type mappings work, so I'm not too enthusiastic to work on it.

  5. EricStG commented on Jan 17, 2019

    @EricStG
    Author

    Just to clarify, in this case, we are using EF Core, but also using a 3rd party library using Npgsql with ADO.Net

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions