1
I’ve read something and I know it’s possible to leave one connectionString dynamically in an ASP.NET MVC application. However, it is possible to create n connections where each authenticated user in the system has its own connection to a particular database?
Do you know that the banks are identical, just change the location of each.
If anyone knows another way to resolve this situation suggestions will be welcomed, because at my level of knowledge, a web application only has one connection, where n users do the queries, recordings, etc... and if you change this connection changes to all users who are accessing the same.
#EDIT - 12/08 15:18
I got some head start. As I use Entity, follow what I got:
public class Entidades : DbContext
{
public static string teste()
{
return HttpContext.Current.Session ["conString"] != null ? (string)HttpContext.Current.Session["conString"] : "Conexao";
}
public Entidades() : base(teste()) {}
public DbSet<Usuario> Usuario { get; set; }
public DbSet<Clientes> Clientes { get; set; }...
}
This Session is created at the moment after the user login, where in the column of the user table of the main database is the name of connectionString, store the name in the session and use the same.
However another question arises, how do I so that according to the insertion or an update of new users and their connectionString, is inserted automatically in the web.config Connections? Is there such a possibility?
It is possible yes, just create multiple conections on the web.config or generate them by concatenating strings.
– Ricardo
In the option to generate the string, how would I declare the Provider? And today I use the Entity to make the connection
– Matheus Silva
When creating your Entities you call the above constructor by passing the string
: base("stringdeconexao")
. I believe that’s what you want.– Ricardo
I tried that, but when I put the string(Server=localhost;Database=DATABASE;User Id=postgres;Password=123456;) and not the same name as in web.config, the Entity generates an exception something like: "Invalid keyword server", because it does not recognize the Provider="Npgsql".
– Matheus Silva
Strange, the name of the constructor variable is
nameOrConnectionString
. Have you ever tried to take a connection string that works on the web.config and play exactly as it is in the constructor? Sometimes the string is wrong.– Ricardo
Yes, I’ve tested it that way, I thought it was strange too... But I believe that if you omit Provider, it looks for Sqlserver, which unlike Npgsql does not use Server but Datasource...
– Matheus Silva