Finbuckle.MultiTenant icon indicating copy to clipboard operation
Finbuckle.MultiTenant copied to clipboard

MultiTenantDbContext constructor with ITenantInfo in V9.0

Open acorbos opened this issue 7 months ago • 5 comments

Hi, I'm having trouble upgrading to 9.0. We were using the constructor of MultiTenantDbContext with parameter ITenantInfo. This was removed. I understand the replacement would be to use the method MultiTenantDbContext.Create, but this fails with "'The provided DbContext type does not have a constructor that accepts the required parameters.'", because our DBContext has additional parameters in its constructor. Any idea how to work arround this? Thanks, Anca

acorbos avatar May 20 '25 11:05 acorbos

Hello, can you post what your db context constructor does look like?

AndrewTriesToCode avatar May 20 '25 13:05 AndrewTriesToCode

Some of our DB tables have audit columns like CreatedBy, CreatedOn, ModifiedBy, etc. We have a base DbContext that sets the audit columns to the current user's ID and current time stamp on SaveChanges, it looks like this:

public class MultitenancyAuditDbContext : MultiTenantDbContext
{
    protected readonly ICurrentUser _currentUser;
    protected MultitenancyAuditDbContext(ITenantInfo tenantInfo, DbContextOptions options, ICurrentUser currentUser) : base(tenantInfo, options) { _currentUser = currentUser; }
[...]
}

acorbos avatar May 20 '25 14:05 acorbos

Ok I will be adding more support for the Create factory method in the next release (a week or two). All it does is create a static IMultiTenantContextAccessor with the desired tenant and create the db context with it. If you need a quick workaround take a loo at that function and make one similar that also includes your DI needs.

AndrewTriesToCode avatar May 21 '25 14:05 AndrewTriesToCode

Just put out a release with more factory methods including one that can take arbitrary dependency instances as well as as service provider. Let me know if that helps.

AndrewTriesToCode avatar Jun 03 '25 04:06 AndrewTriesToCode

Thank you, I will get back to you next week, when I have the chance to upgrade. Meanwhile I created my own factory method.

acorbos avatar Jun 06 '25 14:06 acorbos

Sorry for the delay, I could update today and it is better now, thank you!

acorbos avatar Jul 31 '25 17:07 acorbos