[英]ASP.NET Core 6 named connection string not found
I am using .NET 6 and Entity Framework Core 6 in a new ASP.NET Core MVC web app.我在新的 ASP.NET Core MVC web 应用程序中使用 .NET 6 和 Entity Framework Core 6。 I have my connection string set in user-secrets.
我在用户机密中设置了连接字符串。 I was able to perform a
我能够执行
dotnet ef dbcontext scaffold Name=ConnectionStrings:someconn
without any trouble.没有任何麻烦。 My model context was created.
我的 model 上下文已创建。 Now I want to do a simple query, but the builder in
program.cs
is complaining about not finding the connection.现在我想做一个简单的查询,但是
program.cs
中的构建器抱怨找不到连接。
Here I create a builder在这里我创建一个构建器
var builder = WebApplication.CreateBuilder(args);
var currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
var environmentName = builder.Environment.EnvironmentName;
builder.Configuration
.SetBasePath(currentDirectory)
.AddUserSecrets<Program>()
.AddEnvironmentVariables();
var connectionString = builder.Configuration.GetConnectionString("ConnectionStrings:someconn ");
Here I place this code inside program.cs, and just before app.Run.在这里,我将此代码放在 program.cs 中,就在 app.Run 之前。
using (var ctx = new ModelContext())
{
List<SomeModel> pd = ctx.SomeModel
.Where(p => p.Id == "12345").ToList();
}
app.Run();
Error错误
A named connection string was used, but the name 'ConnectionStrings:someconn' was not found in the application's configuration
使用了命名的连接字符串,但在应用程序的配置中找不到名称“ConnectionStrings:someconn”
Here's a sample for reading a connection string from appsettings.json.下面是从 appsettings.json 读取连接字符串的示例。 (for simplicity I've used a sample for sqlite, which shouldn't affect they general way of reading the string.)
(为简单起见,我使用了 sqlite 的样本,这不应该影响他们读取字符串的一般方式。)
Hope this helps.希望这可以帮助。
appsettings.json应用程序设置.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.AspNetCore": "Information"
}
},
"ConnectionStrings": {
"someconn": "Data Source=some.sqlite"
}
}
and the in Program.cs和 Program.cs 中的
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("someconn")
A reason could be that the appsettings.json
isn't found.原因可能是找不到
appsettings.json
。 Maybe check if this part of the code is needed or execute it after reading the connection string.也许检查这部分代码是否需要或在读取连接字符串后执行它。
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("someconn")
var currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
var environmentName = builder.Environment.EnvironmentName;
builder.Configuration
.SetBasePath(currentDirectory)//check if this is necessary
.AddUserSecrets<Program>()
.AddEnvironmentVariables();
Change below code更改以下代码
var connectionString = builder.Configuration.GetConnectionString("ConnectionStrings:someconn ");
to到
var connectionString = builder.Configuration.GetConnectionString("someconn");
It works in my local, you can try it.它在我的本地有效,你可以试试。 Also note that there is an extra space in your code.
另请注意,您的代码中有一个额外的空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.