Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use DbContextFactory in Blazor Server when using database per tenant? #910

Open
cryo75 opened this issue Nov 28, 2024 · 2 comments
Open
Labels

Comments

@cryo75
Copy link

cryo75 commented Nov 28, 2024

I moved from a single databases to the per-tenant database in a Blazor Server-side app. Currently I have:

services.AddDbContextFactory<AppContext>(options =>
{
    options.UseSqlServer(configuration.GetConnectionString("App"));
});

But now how am I going to change the above service registration to use the per-tenant connection string?

@AndrewTriesToCode
Copy link
Contributor

Hi, it's not using dbcontext factory but if you want to spin up a multitenant db context instance for a given tenant a static create factory method was recently added. Here is the link to the docs:
https://www.finbuckle.com/MultiTenant/Docs/v9.0.0/EFCore#factory-instantiation

Does that get you what you want?

@AndrewTriesToCode
Copy link
Contributor

Also, there is an overload of AddDbContextFactory which accepts a service provider. Using that you can get the tenant by resoving IMultiTenantContextAccessor<TTenantType> then getting the connection string from the current tenant and plugging it into you UseSqlServerLine. That might be better for your use case unless you need to spin up a dbcontext for multiple tenants at once, then the option I described in the prior response will be better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants