DatabasesConnectionStrings

DatabasesConnectionStrings

本文介绍了如何在多个连接字符串中使用optionsBuilder.UseNpgsql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用多个连接字符串在Package Manager控制台中创建一个更新数据库,这些连接字符串存储在名为

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

如果我在字符串列表中指定哪个索引,然后在程序包管理器中运行update-database,它将很好地工作控制台。

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!

推荐答案

这不是最佳解决方案,但它可以满足我的要求……第一步是我生成了TXT文件,其中包含每个数据库的所有连接字符串。

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.

我创建了一个名为ConnectionStringsContext的类

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());
    }
}

从那里我将使用Package Manager Console进行伪造更新:P,因为我想生成我的DatabasesConnectionStrings.txt ...,所以我键入 Update-Database -context ConnectionStringsContext 。 PM完成后将显示一条错误消息,因此将其忽略即可,最终结果是我希望生成的TXT文件具有所有连接字符串。

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.

完成后,我创建了一个名为DatabaseRecordsContext的其他类文件,其中包含我的所有dbset,并且包含以下代码。

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);
            }

        }

我创建了一个PowerShell文件称为UpdateDatabases.ps1,在其中进行了无限循环:

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

这将运行更新数据库-context DatabaseRecordsContext,直到它们不再是我的DatabasesConnectionStrings.txt文件中的连接字符串,并最终用我创建的任何迁移更新所有具有相同架构的数据库

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

这篇关于如何在多个连接字符串中使用optionsBuilder.UseNpgsql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 00:41