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();
}
}
}
using Npgsql;
,using Npgsql.NodaTime
but it didn't work. I have postedStartup.cs
in my question. – phqbInstall-Package NodaTime
from package manager console or search NodaTime in Nuget Package Manager and install it – vivek nuna