Sto utilizzando Entity Framework Core per lavorare con un database PostgreSQL tramite il provider di dati Npgsql. Secondo la guida alla mappatura Date / Time , NodaTime è raccomandato per la mappatura di data / ora PostgreSQL. Nella guida all'installazione, il codice seguente abilita la mappatura del tipo NodaTime:
protected override void OnConfiguring(DbContextOptionsBuilder builder) { builder.UseNpgsql("Host=localhost;Database=test;Username=npgsql_tests;Password=npgsql_tests", o => o.UseNodaTime()); }
Ma non esiste un metodo di estensione UseNodaTime()
per NpgsqlDbContextOptionsBuilder
. Ho cercato il codice sorgente di npgsql
ma non ho trovato il metodo di estensione. L'unico che ho trovato era public static INpgsqlTypeMapper UseNodatime(this INpgsqlTypeMapper mapper)
in questo file .
Il mio .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();
}
}
}
È necessario aggiornare tutto alla versione 4.0.0-rc1 e installare il pacchetto Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime . Ho segnalato il problema qui
La documentazione dal link non è corretta (il tipico pasticcio di pre-release). Si afferma che è necessario il pacchetto Npgsql.NodaTime
mentre in realtà è necessario il pacchetto Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime
:
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="2.1.0-rc1" />