简体   繁体   English

在实体框架核心中使用Npgsql.NodaTime

[英]Using Npgsql.NodaTime in Entity Framework Core

I'm using Entity Framework Core to work with a PostgreSQL Database via Npgsql Data Provider. 我正在使用Entity Framework Core通过Npgsql数据提供程序与PostgreSQL数据库一起使用。 According to Date/Time mapping guide , NodaTime is recommended for PostgreSQL date/time mapping. 根据日期/时间映射指南 ,建议将NodaTime用于PostgreSQL日期/时间映射。 In setup guide, the following code enables NodaTime type mapping: 在安装指南中,以下代码启用NodaTime类型映射:

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 . 但没有UseNodaTime()的扩展方法NpgsqlDbContextOptionsBuilder I searched npgsql source code but didn't find that extension method. 我搜索了npgsql源代码,但没有找到该扩展方法。 The only one I found was public static INpgsqlTypeMapper UseNodatime(this INpgsqlTypeMapper mapper) in this file . 我发现的唯一文件此文件中的 public static INpgsqlTypeMapper UseNodatime(this INpgsqlTypeMapper mapper)

My .csproj : 我的.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();
    }
  }
}

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: 它指出您需要Npgsql.NodaTime软件包,而实际上您需要Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime软件包:

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

You need to upgrade everything to the 4.0.0-rc1 version and install the Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime package. 您需要将所有内容升级到4.0.0-rc1版本,并安装Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime软件包。 I have reported the issue here 我已经在这里报告了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM