Using Npgsql.NodaTime in Entity Framework Core

asp.net-core-webapi c# entity-framework-core nodatime npgsql

Question

I'm using Entity Framework Core to work with a PostgreSQL Database via Npgsql Data Provider. According to Date/Time mapping guide, NodaTime is recommended for PostgreSQL date/time mapping. In setup guide, the following code enables NodaTime type mapping:

protected override void OnConfiguring(DbContextOptionsBuilder builder) { builder.UseNpgsql("Host=localhost;Database=test;Username=npgsql_tests;Password=npgsql_tests", o => o.UseNodaTime()); }

But there is no UseNodaTime() extension method for NpgsqlDbContextOptionsBuilder. I searched npgsql source code but didn't find that extension method. The only one I found was public static INpgsqlTypeMapper UseNodatime(this INpgsqlTypeMapper mapper) in this file.

My .csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.1.0-rc1-final" />
    <PackageReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.0-rc1-final" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.1.0-rc1" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" Version="2.1.0-rc1" />
    <PackageReference Include="Npgsql.NodaTime" Version="1.0.0-rc1" />
  </ItemGroup>

</Project>

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.EntityFrameworkCore;
using Doko.Models;
using Doko.Filters;
using Npgsql;

namespace DokoDoko {
  public class Startup {
    public Startup(IConfiguration configuration) {
      Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services) {
      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
      services.AddEntityFrameworkNpgsql().AddDbContext<DokoContext>(
        options => options.UseNpgsql(
          Configuration.GetConnectionString("DokoDatabase"), 
          o => { 
            o.UseNetTopologySuite();
          }
        )
      );
      services.AddCors();
      services.AddScoped<AuthorizationFilter>();
      NpgsqlConnection.GlobalTypeMapper.UseNodatime();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
      if (env.IsDevelopment()) {
        app.UseDeveloperExceptionPage();
      } else {
        app.UseHsts();
      }

      app.UseHttpsRedirection();
      app.UseMvc();
    }
  }
}
1
2
5/23/2018 2:37:20 AM

Accepted Answer

You need to upgrade everything to the 4.0.0-rc1 version and install the Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime package. I have reported the issue here

1
5/23/2018 8:55:11 AM

Popular Answer

The documentation from the link is incorrect (the typical pre release mess). It states that you need Npgsql.NodaTime package while in fact you need Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime package:

<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="2.1.0-rc1" />


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow