ApplicationDbContext can't be used in WithEFCoreStore (TEFCoreStoreDbContext error)
See original GitHub issueHi there,
first thank you for the great project. I’m using .NET 5 and try to combine WithEFCoreStore
with WithClaimStrategy
. Having two DB contexts only the first one has Tenant information.
public class MultiTenantStoreDbContext : EFCoreStoreDbContext<TenantInfo>
public class ApplicationDbContext : IdentityDbContext, IMultiTenantDbContext
services.AddMultiTenant<TenantInfo>()
.WithEFCoreStore<MultiTenantStoreDbContext, TenantInfo>()
.WithClaimStrategy()
.WithPerTenantAuthentication();
However this also requires multiple migration folders and you can’t use transactions in register method when Tenant, User and Claim should be created in a safe way. I tried to combine this in in one context but this results in an type error:
public class ApplicationDbContext : IdentityDbContext, IMultiTenantDbContext
or (both produce the same error)
public class ApplicationDbContext : MultiTenantIdentityDbContext
services.AddMultiTenant<TenantInfo>()
.WithEFCoreStore<ApplicationDbContext , TenantInfo>()
.WithClaimStrategy()
.WithPerTenantAuthentication();
Error
CS0311
The type 'ApplicationDbContext' cannot be used as type parameter
'TEFCoreStoreDbContext' in the generic type or method
'FinbuckleMultiTenantBuilderExtensions.WithEFCoreStore<TEFCoreStoreDbContext, TTenantInfo>(FinbuckleMultiTenantBuilder<TTenantInfo>)'.
There is no implicit reference conversion from 'ApplicationDbContext' to
'Finbuckle.MultiTenant.Stores.EFCoreStoreDbContext<Finbuckle.MultiTenant.TenantInfo>'
Shouldn’t TEFCoreStoreDbContext
be IMultiTenantDbContext
?
Issue Analytics
- State:
- Created 3 years ago
- Comments:14 (7 by maintainers)
Top Results From Across the Web
MultiTenant Stores
The database context used with the EFCore store must derive from EFCoreStoreDbContext , but other entities can be added:
Read more >DbContext Lifetime, Configuration, and Initialization
This example registers a DbContext subclass called ApplicationDbContext as a scoped service in the ASP.NET Core application service provider ( ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks for the detail. I didn’t anticipate that the security stamp validation would call into the db context but this makes sense.
The fix is to move line 76 on the snippet below down below the check for validation bypass. This is safe to do since this call to authenticate is not used to set the actual user – just to check the tenant claim after which the results are discarded. The actual signin middleware will do the real validation and reject the user if the security stamp is invalid. The tenant would still be set though so be aware of that.
I’ll include the fix in the next release. Thanks for you help.
https://github.com/Finbuckle/Finbuckle.MultiTenant/blob/baa26127ca5813c11d270369e24ccf2aea379bf2/src/Finbuckle.MultiTenant.AspNetCore/Extensions/MultiTenantBuilderExtensions.cs#L73-L80
That sounds like a good behavior. But pages without authentication would just have the tenant set to null right?
For this project: It’s one of the kind you build in your spare time to hope to release it one day 😉 In my professional life I’m working with node/react mostly atm however we use a lot of dependencies. For some of those I would really like an option to pay some money to get some problems fixed or at least PRs merged in time.