问题
I am trying to do an update-database in the Package Manager Console with multiple connection strings that are stored in a list of strings called
lDataBaseConnection
It works fine if I specify which index in my list of strings and simply run the update-database in the Package Manager Console.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(this.lDataBaseConnection[0]);
}
But if I use a loop, instead of updating all the databases... I only get an update-database on the last index of my list of strings.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
for (int indexOflDataBaseConnection = 0; indexOflDataBaseConnection < lDataBaseConnection.Count; indexOflDataBaseConnection++)
{
optionsBuilder.UseNpgsql(this.lDataBaseConnection[indexOflDataBaseConnection]);
}
}
how can I solve this problem? Thanks!
回答1:
It's not the BEST solution but it does what I want...the first step is that I generat a TXT file that contains all the connection strings of each databases.
I've created a Class called ConnectionStringsContext
public class ConnectionStringsContext : DbContext
{
public System.Text.StringBuilder ConnectionString = new System.Text.StringBuilder(10000);
public ConnectionStringsContext()
{
UsersRecordsContext oContext = new UsersRecordsContext();
List<Users> lUsers = oContext.Users.ToList();
for (int indexOflUsers = 0; indexOflUsers < lUsers.Count; indexOflUsers++)
{
ConnectionString.AppendLine("Server=localhost; User Id=postgres; Password=Password; port=5432; Database=" + lUsers[indexOflUsers].usernameid.ToUpper() + "_DB; Pooling=false;");
}
System.IO.File.WriteAllText("DatabasesConnectionStrings.txt", ConnectionString.ToString());
}
}
From there I would use the Package Manager Console to fake an update :P because I want to generate my DatabasesConnectionStrings.txt... so I type Update-Database -context ConnectionStringsContext
. PM will show an error message when done so just ignore it, the end result is that I want my generated TXT file with all the connections string.
once done I've created a different class file called DatabaseRecordsContext which has all my dbset and I've included the following code.
public String DataBaseConnection;
public DatabaseRecordsContext ()
{
try
{
System.IO.StreamReader ReadFile = new System.IO.StreamReader("DatabasesConnectionStrings.txt");
DataBaseConnection = ReadFile.ReadLine();
System.Console.WriteLine(DataBaseConnection);
string RestOfConnectionStrings = ReadFile.ReadToEnd();
ReadFile.Close();
System.IO.File.WriteAllText("DatabasesConnectionStrings.txt", RestOfConnectionStrings);
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
}
}
I've created a PowerShell file Called UpdateDatabases.ps1 and in inside of it I did an infinit loop:
While($true){update-database -context DatabaseRecordsContext}
Now back to my Package Manager Console I simply run the command
./UpdateDatabases.ps1
This will run the update-database -context DatabaseRecordsContext until they are no more connection strings in my DatabasesConnectionStrings.txt file and in the end updates all my databases that have the same schema with any migration that i've created
来源:https://stackoverflow.com/questions/62398191/how-to-use-the-optionsbuilder-usenpgsql-with-multiple-connection-strings