gpt4 book ai didi

c# - 在 Entity Framework Core 中使用 Npgsql.NodaTime

转载 作者:行者123 更新时间:2023-11-30 20:26:06 31 4
gpt4 key购买 nike

我正在使用 Entity Framework Core 通过 Npgsql 数据提供程序来处理 PostgreSQL 数据库。根据Date/Time mapping guide ,建议将 NodaTime 用于 PostgreSQL 日期/时间映射。在设置指南中,以下代码启用 NodaTime 类型映射:

protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
builder.UseNpgsql("Host=localhost;Database=test;Username=npgsql_tests;Password=npgsql_tests",
o => o.UseNodaTime());
}

但是 NpgsqlDbContextOptionsBuilder 没有 UseNodaTime() 扩展方法。我搜索了 npgsql 源代码,但没有找到该扩展方法。我在 this file 中找到的唯一一个是 public static INpgsqlTypeMapper UseNodatime(this INpgsqlTypeMapper mapper) .

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

最佳答案

链接中的文档不正确(典型的预发布困惑)。它声明您需要 Npgsql.NodaTime打包,而实际上你需要 Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime包裹:

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

关于c# - 在 Entity Framework Core 中使用 Npgsql.NodaTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50479014/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com