Код: Выделить всё
Configuration.GetConnectionString("DefaultConnection")
Вот моя настройка - appsettings.json :
Код: Выделить всё
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=MyDb;Trusted_Connection=True;"
}
}
< /code>
Program.csКод: Выделить всё
var builder = WebApplication.CreateBuilder(args);
// Trying to get connection string
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
Console.WriteLine("Connection String: " + connectionString);
// Add services
builder.Services.AddDbContext(options =>
options.UseSqlServer(connectionString));
< /code>
Console output:
Connection String: (null)
< /code>
What I've tried:
[list]
[*]Confirmed appsettings.json[*] Проверено обшивка ConnectionStrings и defaultconnection
[*] Пробое Builder.configuration ["connectionStrings: DefaultConnection"] все еще nul builder.host.configureappconfiguration (...) -> без изменения
[/list]
Ожидаемое поведение:
Я ожидаю
Код: Выделить всё
builder.Configuration.GetConnectionString("DefaultConnection")
< /code>
to return the connection string defined in appsettings.jsonСреда:
asp.net core 8.0
[*] Windows 11
[*] Visual Studio 2022 (последний)
[*].net sdk 8.0.100
/> Вопрос : Почему GetConnectionString ("defaultConnection") возвращение нуля, даже если ключ существует в appsettings.json , и как я его исправить?
Подробнее здесь: https://stackoverflow.com/questions/797 ... ite-appset
Мобильная версия