Is there some example about setting the dbcontext connection string after identity user logs in?
I'm thinking about sending some user key when calling Login method and then setting some DI service property, for example ProgramConfig.ConnectionString....but how can I access it from the dbcontext constructor?
Is there some example about setting the dbcontext connection string after identity user logs in?
You can create a dbcontext factory and call different connection string according to the login user:
public static class DbContextFactory
{
public static Dictionary<string, string> ConnectionStrings { get; set; }
public static void SetConnectionString(Dictionary<string, string> connStrs)
{
ConnectionStrings = connStrs;
}
public static MyDbContext Create(string connid) //judge which connectionstring to use by connid, from Configure()
{
if (!string.IsNullOrEmpty(connid))
{
var connStr = ConnectionStrings[connid];
var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
optionsBuilder.UseSqlServer(connStr);
return new MyDbContext(optionsBuilder.Options);
}
else
{
throw new ArgumentNullException("ConnectionId");
}
}
}
and in Configure():
Dictionary<string, string> connStrs = new Dictionary<string, string>();
connStrs.Add("DB1", "Server=***;Database=***;***;MultipleActiveResultSets=true");
connStrs.Add("DB2", Your connection string);
DbContextFactory.SetConnectionString(connStrs);
then the way to use:
if(user is login) {... var dbContext = DbContextFactory.Create("DB1");
var model = dbContext.Gate.ToList();
//......as usual } if(user is **) { ... var dbContext = DbContextFactory.Create("DB2"); ... }
Best Regards,
Jerry Cai
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
You do not have to change the dbcontext constructor, this factory just offers a way to select which database too use. you can control which
database connection string to use (or not) after identity user logs in.
Best Regards,
Jerry Cai
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
1 Points
10 Posts
Set dynamic dbcontext connection string after AspNetUser logs in
Dec 28, 2020 10:46 PM|bernardj1905|LINK
Is there some example about setting the dbcontext connection string after identity user logs in?
I'm thinking about sending some user key when calling Login method and then setting some DI service property, for example ProgramConfig.ConnectionString....but how can I access it from the dbcontext constructor?
Thanks!
Participant
780 Points
260 Posts
Re: Set dynamic dbcontext connection string after AspNetUser logs in
Dec 29, 2020 09:46 AM|Jerry Cai|LINK
Hi,bernardj1905
You can create a dbcontext factory and call different connection string according to the login user:
and in Configure():
Dictionary<string, string> connStrs = new Dictionary<string, string>(); connStrs.Add("DB1", "Server=***;Database=***;***;MultipleActiveResultSets=true"); connStrs.Add("DB2", Your connection string); DbContextFactory.SetConnectionString(connStrs);
then the way to use:
Best Regards,
Jerry Cai
Member
1 Points
10 Posts
Re: Set dynamic dbcontext connection string after AspNetUser logs in
Dec 29, 2020 08:33 PM|bernardj1905|LINK
Hi Jerry!
Thanks for the answer, I'll try with your advice. Just, how can I use the factory in the dbcontext constructor?
Thanks!
Bernard.
Participant
780 Points
260 Posts
Re: Set dynamic dbcontext connection string after AspNetUser logs in
Dec 30, 2020 06:23 AM|Jerry Cai|LINK
Hi,bernardj1905
You do not have to change the dbcontext constructor, this factory just offers a way to select which database too use. you can control which
database connection string to use (or not) after identity user logs in.
Best Regards,
Jerry Cai