How do I use a different connection string with my DbContext class & for migrations?

41 views Asked by At

We have different DB users (with differing permissions) that we use for read/write operations (readwrite) and for creating/modifying tables (modifytable).

I want to use a connection string with readwrite user for the DbContext class and was able to do it this way in the Program.cs file:

builder.Services.AddDbContext<ReadWriteContext>(
    options => options.UseSqlServer(rwConnString));

How do I do the equivalent for the migrations part (i.e., giving it a connection string with the user modifytable)?

All the examples I've seen simply talk about running the following command in NuGet Package Manager Console:

Add-Migration InitialCreate

and then

Update-Database

How (& from where) does this Update-Database pick up the connection string? How do I supply a custom connection string?

We construct the connection string using some logic that takes some values from Azure Key Vault, so reading it from a configuration file is not an option.

I'm brand new to EF.

1

There are 1 answers

0
markvgti On BEST ANSWER

Found out the solution to my problem: the Update-Database command takes a connection string:

Update-Database -connection <connection_string>