gpt4 book ai didi

asp.net-core - 带有 Postgres 连接字符串错误的 EF Core

转载 作者:行者123 更新时间:2023-12-05 04:11:36 28 4
gpt4 key购买 nike

我第一次尝试将 ASP.net Core 与 postgres 一起使用。我正在关注 ( http://dotnetthoughts.net/using-postgresql-with-aspnet-core/) 这篇博文来创建我的第一次迁移。我已经在我的 posgresSQL 中创建了一个名为 asp_api_test 的数据库。

我的 dotnet ef migrations add Initial 成功了。但是,当我运行 dotnet ef database update 时遇到错误 Format of the initialization string does not conform to specification starting at index 0

我的 appsettings.json:

{
"ConnectionStrings": {
"DataAccessPostgreSqlProvider": "Host=localhost;Username=postgres;Password=root;Database=asp_trial_api;Pooling=true;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}

我猜我的连接字符串不正确。我的 startup.cs:

public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);

services.AddDbContext<WebAPIDataContext>(options => {
options.UseNpgsql("DataAccessPostgreSqlProvider", b => b.MigrationsAssembly("New_Api"));
});

services.AddMvc();
}

我的项目.json:

{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.EntityFrameworkCore": "1.0.0",
"Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final",
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview1-final",
"imports": [
"portable-net45+win8+dnxcore50",
"portable-net45+win8"
]
},
"Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.0",
"Microsoft.ApplicationInsights.AspNetCore": "2.0.0"
},

"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
},

"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},

"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},

"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},

"publishOptions": {
"include": [
"wwwroot",
"**/*.cshtml",
"appsettings.json",
"web.config"
]
},

"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}

我做错了什么?

最佳答案

我更新到以下-

应用设置.js:

{
"Data": {
"DefaultConnection": {
"ConnectionString": "Host=localhost;Username={{postgres}};Password={{root}};Database={{asp_trial_api}}"
}
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}

在我的 startup.cs 中:

services.AddDbContext<WebAPIDataContext>(options => {
options.UseNpgsql(Configuration["Data:DefaultConnection:ConnectionString"]);
});

关于asp.net-core - 带有 Postgres 连接字符串错误的 EF Core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42448882/

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