Код: Выделить всё
var services = new ServiceCollection();
builder.Services.AddDbContext(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
Я проверил то же самое с Copilot, он дал мне этот код, но он ошибки AT:
Код: Выделить всё
builder.Configuration.AddAzureAppConfiguration();
builder.Configuration.Build();
-------------------
using Microsoft.Extensions.Configuration;
var builder = WebApplication.CreateBuilder(args);
// Add Azure App Configuration
builder.Configuration.AddAzureAppConfiguration(Environment.GetEnvironmentVariable("DefaultConnection"));
// Build the configuration
var config = builder.Configuration.Build();
// Access the connection string
string connectionString = config.GetConnectionString("YourConnectionStringName");
Я также попробовал этот код, но я не могу получить строку подключения:
Код: Выделить всё
string connString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext(options =>
options.UseSqlServer(connString));
Из -за этого я получаю эту ошибку:
. Неправомерное исключение, когда обработано. /> InvalidoperationException: свойство ConnectionString не было инициализировано.
microsoft.data.sqlclient.sqlconnection.permissionDemand() Proted ucalled 'Exception " src = "https://i.sstatic.net/an72vp8j.png"/>
help>
Подробнее здесь: https://stackoverflow.com/questions/795 ... p-in-net-8
Мобильная версия