Skip to content

Can't write CLR type NpgsqlTypes.NpgsqlRange`1[System.DateTime] with handler type RangeHandler #648

Closed

Description

@baurzhan

Simple test case throws exception

// model 
    public class Test
    {
        public int Id { get; set; }
        public NpgsqlRange<DateTime> Valid { get; set; }
    }
    // dbcontext configuration
    public virtual DbSet<App.Test> Tests { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
             modelBuilder.Entity<Test>(entity =>
            {
                entity.ToTable("test", "app");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Id).HasColumnName("id");
                entity.Property(e => e.Valid).HasColumnName("valid");
            });
     // migration
     protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.EnsureSchema(
                name: "app");
            migrationBuilder.CreateTable(
                name: "test",
                schema: "app",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false),
                    Valid = table.Column<NpgsqlRange<DateTime>>(nullable: false)
               },
                constraints: table =>
                {
                    table.PrimaryKey("test_pk", x => new { x.Id});
                });
        }
        // create new entity
           var options = new DbContextOptionsBuilder<OptiroContext>()
                .UseNpgsql(_connectionString, ob => ob.UseNodaTime().UseNetTopologySuite())
                .UseLoggerFactory(Global.ConsoleLoggerFactory).Options;
            using (var context = new OptiroContext(options))
            {
               
                DateTime validFrom = new DateTime(2018, 09, 21, 0,0,0);
                DateTime validTo = DateTime.MaxValue;
                context.Tests.Add(new Test
                {
                    Valid = new NpgsqlRange<DateTime>(validFrom, validTo)
                });
                context.SaveChanges();
            }

stack trace

Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while updating the entries. See the inner exception for details.
---- System.InvalidCastException : Can't write CLR type NpgsqlTypes.NpgsqlRange`1[System.DateTime] with handler type RangeHandler`1
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(DbContext _, ValueTuple`2 parameters)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded) in C:\projects\npgsql-entityframeworkcore-postgresql\src\EFCore.PG\Storage\Internal\NpgsqlExecutionStrategy.cs:line 51
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Optiro.Data.OptiroContext.SaveChanges(Boolean acceptAllChangesOnSuccess) in C:\PROJECTS\test\dbContext.cs:line 3145
   at Optiro.Data.Tests.AuditTest.Test1() in C:\PROJECTS\test\Test.cs:line 80

package refererences

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.1.1" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.1.2" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.1" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" Version="2.1.1" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="2.1.1" />
    <PackageReference Include="Npgsql.Json.NET" Version="1.0.0" />
  </ItemGroup>

Activity

  1. austindrenski commented on Sep 21, 2018

    @austindrenski
    Contributor

    By design, the BCL date/time types are not supported when the NodaTime plugin is configured.

    I'm going to close this for now, but please feel free to post-back if you have further questions.

  2. roji commented on Sep 23, 2018

    @roji
    Member

    @austindrenski FYI I completely removed the invalid tag... We used to have this in Npgsql too, but it seems to make more sense to just leave invalid issues untagged. So any issue that's closed and untagged implicitly did not mature into a feature/bug/whatever. Is that OK with you?

  3. austindrenski commented on Sep 23, 2018

    @austindrenski
    Contributor

    @roji That totally works for me. I was just trying follow previous patterns, but thinking about it now, marking a well-intentioned question as invalid does seem a bit.... unwelcoming?

    So that's OK with me!

  4. baurzhan commented on Sep 27, 2018

    @baurzhan
    Author

    Can you add notice about this restriction in NodaTime Type Plugin documentation page?

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