question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ApplicationDbContext can't be used in WithEFCoreStore (TEFCoreStoreDbContext error)

See original GitHub issue

Hi 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:closed
  • Created 3 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
AndrewTriesToCodecommented, Mar 14, 2021

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

1reaction
spech66commented, Mar 14, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found