S
S
sdfdgfhgfh2018-10-14 15:22:37
ASP.NET
sdfdgfhgfh, 2018-10-14 15:22:37

Two or more connection and session strings?

There is a simple site that displays some information in descending order from the database tables. There is also a registration system, password change, etc. There was a need to use this site on 2-3 different databases (the same in structure) that lie on different machines. That is, in fact, you need to implement the following: in the header, poke the server 1 button (for example) and receive data from the server 1 database, register, change the password, etc. Near the server 2 button and everything is similar, but with the base, which lies on another server. User authorization is based on Claims and it is important that they are "inside" the connection string they have chosen until they decide to go to another database and click on the appropriate button.
How is this generally implemented and in which direction to dig then? If possible, an example. Thank you all very much for any information!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2018-10-14
@kttotto

You can make as many string connections as you like in the settings, each with its own name. Make an abstraction over the connection to the database, you will pass the name of the connection there and work with the methods of this abstraction already.
In simple terms, it's like this

<connectionStrings>
  <add name="DB1Connection" connectionString="data source=(local);initial catalog=myDB1;" />
  <add name="DB2Connection1" connectionString="data source=(local);initial catalog=myDB2;" />
  <add name="DB3Connection1" connectionString="data source=(local);initial catalog=myDB3;" />
</connectionStrings>

connectionStringName = "DB3Connection1";

public class MyDbContext : DbContext
{
  public MyDbContext(string connectionStringName) : base(connectionStringName)
  {	}
}

If the database schemas are the same, then one context class is enough.
As soon as the user clicked on another server, you logged it out, created a new connection and logged in again, but in the selected context, and reloaded the page.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question