* Assign Tenant.AgnosticTenantId in populator * Corrected comment * Fix entities persisted with NULL TenantId instead of default tenant Use normalized TenantId property (which returns empty string for default tenant) instead of Tenant?.Id (which returns null when no tenant is set). When Tenant?.Id returned null: TenantAwareDbContextFactory set dbContext.TenantId to null, ApplyTenantId skipped assigning TenantId, entities were saved with NULL TenantId, and query filters using SQL equality against NULL never matched these entities. This affected all EF Core stores including BookmarkStore and any store using bulk upsert operations. Co-authored-by: Cursor <cursoragent@cursor.com> * Update src/modules/Elsa.Workflows.Runtime/Services/DefaultWorkflowDefinitionStorePopulator.cs Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * PR comments * Set Tenant.AgnosticTenantId when no tenantId is specified --------- Co-authored-by: Ralf <Ralf@Careconnections.nl> Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
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.TenantId;
|
|
}
|
|
} |