2
votes

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();
    }
  }
}
2
So whats your question?vivek nuna
I'm trying to do as this code but the compiler didn't find UseNodaTime() extension method.phqb
What is the error? Are you missing any using ?vivek nuna
I tried using Npgsql;, using Npgsql.NodaTime but it didn't work. I have posted Startup.cs in my question.phqb
Try Install-Package NodaTime from package manager console or search NodaTime in Nuget Package Manager and install itvivek nuna

2 Answers

3
votes

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" />
1
votes

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