Проблема с подключением к БД "Значение не может быть нулевым. (Параметр connectionString ')" (Идентичность ASP. NET) - PullRequest
0 голосов
/ 25 апреля 2020

Я пытаюсь установить соединение с БД, размещенной на Azure, но у меня есть некоторые проблемы с кодом, когда я запускаю приложение, это приводит к ошибке в параметре: connectionString.

STARTUP.CS

{
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.AddDbContext<Contexto>
        (
            options => options.UseSqlServer("Data Source=Exemple.database.windows.net;Initial Catalog=NameDataDB;Persist Security Info=True;User ID=UserDB;Password=***********")
        );
        services.AddDbContext<IdentityContexto>
        (
            options => options.UseSqlServer("Data Source=exemple.database.windows.net;Initial Catalog=NameUserDB;Persist Security Info=True;User ID=userdb;Password=***********")
        );

CONTEXTO.CS

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(configuration.GetConnectionString("NameDB"));
        base.OnConfiguring(optionsBuilder);
    }
}

При запуске приложения возникает следующая ошибка:

ArgumentNullException: Value cannot be null. (Parameter 'connectionString')

1 Ответ

1 голос
/ 25 апреля 2020

configuration.GetConnectionString НЕ получит строку ваших подключений, ЕСЛИ вы не добавите их в appSettings следующим образом:

{"ConnectionStrings": {"BloggingDatabase": "Server = (localdb) \ mssqllocaldb; База данных = EFGetStarted.ConsoleApp.NewDb; Trusted_Connection = True;» },}

...