2

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();
    }
  }
}
8
  • So whats your question? Commented May 23, 2018 at 6:02
  • I'm trying to do as this code but the compiler didn't find UseNodaTime() extension method. Commented May 23, 2018 at 6:08
  • What is the error? Are you missing any using ? Commented May 23, 2018 at 6:12
  • I tried using Npgsql;, using Npgsql.NodaTime but it didn't work. I have posted Startup.cs in my question. Commented May 23, 2018 at 6:18
  • Try Install-Package NodaTime from package manager console or search NodaTime in Nuget Package Manager and install it Commented May 23, 2018 at 6:22

2 Answers 2

4

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" />
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Now the method is found.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.