* Restore EF Core persistence layer Originally, we had moved EF Core to the extensions repo, but in practice this turned out to be rather impractical when debugging elsa-core, given that we would lose all of the state (workflow definitions, instances, etc.) across application restarts. * Integrate PostgreSQL support into EF Core persistence and enhance BlobStorage workflows provider setup.
37 lines
1.2 KiB
C#
37 lines
1.2 KiB
C#
using Elsa.Common.Multitenancy;
|
|
using Elsa.Extensions;
|
|
using JetBrains.Annotations;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Elsa.Persistence.EFCore;
|
|
|
|
/// <summary>
|
|
/// A factory class that wraps and extends the functionality of an existing <see cref="IDbContextFactory{TDbContext}"/>
|
|
/// to provide multi-tenancy support. This ensures that the created DbContext instances are aware of the current tenant context.
|
|
/// </summary>
|
|
[UsedImplicitly]
|
|
public class TenantAwareDbContextFactory<TDbContext>(
|
|
IDbContextFactory<TDbContext> decoratedFactory,
|
|
ITenantAccessor tenantAccessor) : IDbContextFactory<TDbContext>
|
|
where TDbContext : DbContext
|
|
{
|
|
public TDbContext CreateDbContext()
|
|
{
|
|
var context = decoratedFactory.CreateDbContext();
|
|
SetTenantId(context);
|
|
return context;
|
|
}
|
|
|
|
public async Task<TDbContext> CreateDbContextAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
var context = await decoratedFactory.CreateDbContextAsync(cancellationToken);
|
|
SetTenantId(context);
|
|
return context;
|
|
}
|
|
|
|
private void SetTenantId(TDbContext context)
|
|
{
|
|
if (context is ElsaDbContextBase elsaContext)
|
|
elsaContext.TenantId = tenantAccessor.Tenant?.Id.NullIfEmpty();
|
|
}
|
|
} |