0

I have an ASP.NET Core 2.2 project using EF Core that is used by multiple customers. Each customer has their own database with the exact same schema managed by IRepository (Dynamically loaded DBsets). It is multi sub-domain projects, I would like to login uses on account.example.com, after authenticate users will redirected to websites B site.example.com authorize by Cookie Authentication.

Website A, at www.example.com

Website B, at site.example.com -(My application)

Website C, at account.example.com -(ASP.NET Identity -.Net Core 2.2)

public MydbContext(string dbName)
        { 
            this.Database.GetDbConnection().ChangeDatabase(dbName);

        }

But it got default connection string that was added in ConfigureServices in startup.cs class. How can I use Session to store connection string of current user from Cookie in HttpContext.User and change connection string of DBContext per query.

Surya
  • 168
  • 4
  • 19
  • 1
    Possible duplicate of [Configure connection string from controller (ASP.NET Core MVC 2.1)](https://stackoverflow.com/questions/54490808/configure-connection-string-from-controller-asp-net-core-mvc-2-1) –  Jun 14 '19 at 20:58
  • Duplicate of https://stackoverflow.com/questions/36816215/dynamically-change-connection-string-in-asp-net-core – juanvan Jun 15 '19 at 01:26
  • @RuardvanElburg How can I use ***Session*** to store connection string of current user from Cookie in HttpContext.User and change connection string of DBContext by ***per query***. – Surya Jun 15 '19 at 14:07
  • I'm not sure why you would want to store the connectionstring in a cookie or session. How do you set it in the first place? What happens when the user removes the cookie or switches device? You can access the session in DbConnectionInfo: `httpContextAccessor.HttpContext.Session`. If you need to access different databases within one request then think of seperate services that get a different context injected, or think of a factory pattern. –  Jun 15 '19 at 19:48
  • @RuardvanElburg I did every this first time it works but when I redirect to other page the I got error `session has not been configured for this application or request.` – Surya Jun 18 '19 at 06:32
  • @Surya Given this new information I think you should ask a new question where you show the relevant code and more information about the error. Without it I can't say what is happening and why it doesn't work. –  Jun 18 '19 at 07:07
  • Check my answer here for connection based on current request https://stackoverflow.com/questions/36816215/dynamically-change-connection-string-in-asp-net-core/50496810#50496810 – ginalx Dec 09 '19 at 21:03

1 Answers1

1

You can pass the connection string to your constructor and use that. The pattern in EF Core looks like this:

public class Db : DbContext
{
    readonly string connectionString;

    public Db(string connectionString)
    {
        this.connectionString = connectionString;
    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(connectionString);
        base.OnConfiguring(optionsBuilder);
    }
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67