Skip to content

Commit ca9e9fd

Browse files
committedApr 28, 2024
fix: only throw exception in EnforceMultiTenant for null tenant if there are entity changes. (#819)
1 parent e6e792e commit ca9e9fd

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed
 

‎src/Finbuckle.MultiTenant.EntityFrameworkCore/Extensions/IMultiTenantDbContextExtensions.cs ‎src/Finbuckle.MultiTenant.EntityFrameworkCore/Extensions/MultiTenantDbContextExtensions.cs

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
// Refer to the solution LICENSE file for more information.
33

44
using System.Linq;
5+
using Finbuckle.MultiTenant.Abstractions;
56
using Finbuckle.MultiTenant.EntityFrameworkCore;
67
using Microsoft.EntityFrameworkCore;
78

89
// ReSharper disable once CheckNamespace
910
namespace Finbuckle.MultiTenant;
1011

11-
public static class IMultiTenantDbContextExtensions
12+
public static class MultiTenantDbContextExtensions
1213
{
1314
/// <summary>
1415
/// Checks the TenantId on entities taking into account
@@ -17,7 +18,7 @@ public static class IMultiTenantDbContextExtensions
1718
public static void EnforceMultiTenant<TContext>(this TContext context) where TContext : DbContext, IMultiTenantDbContext
1819
{
1920
var changeTracker = context.ChangeTracker;
20-
var tenantInfo = context.TenantInfo;
21+
ITenantInfo tenantInfo = context.TenantInfo!;
2122
var tenantMismatchMode = context.TenantMismatchMode;
2223
var tenantNotSetMode = context.TenantNotSetMode;
2324

@@ -26,14 +27,17 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
2627
Where(e => e.Metadata.IsMultiTenant()).ToList();
2728

2829
// ensure tenant context is valid
29-
if (tenantInfo is null)
30-
throw new MultiTenantException("MultiTenant Entity cannot be changed if TenantInfo is null.");
30+
if (changedMultiTenantEntities.Any())
31+
{
32+
if (tenantInfo == null)
33+
throw new MultiTenantException("MultiTenant Entity cannot be changed if TenantInfo is null.");
34+
}
3135

3236
// get list of all added entities with MultiTenant annotation
3337
var addedMultiTenantEntities = changedMultiTenantEntities.
3438
Where(e => e.State == EntityState.Added).ToList();
3539

36-
// handle Tenant Id mismatches for added entities
40+
// handle Tenant ID mismatches for added entities
3741
var mismatchedAdded = addedMultiTenantEntities.
3842
Where(e => (string?)e.Property("TenantId").CurrentValue != null &&
3943
(string?)e.Property("TenantId").CurrentValue != tenantInfo.Id).ToList();
@@ -71,7 +75,7 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
7175
var modifiedMultiTenantEntities = changedMultiTenantEntities.
7276
Where(e => e.State == EntityState.Modified).ToList();
7377

74-
// handle Tenant Id mismatches for modified entities
78+
// handle Tenant ID mismatches for modified entities
7579
var mismatchedModified = modifiedMultiTenantEntities.
7680
Where(e => (string?)e.Property("TenantId").CurrentValue != null &&
7781
(string?)e.Property("TenantId").CurrentValue != tenantInfo.Id).ToList();
@@ -96,7 +100,7 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
96100
}
97101
}
98102

99-
// handle Tenant Id not set for modified entities
103+
// handle Tenant ID not set for modified entities
100104
var notSetModified = modifiedMultiTenantEntities.
101105
Where(e => (string?)e.Property("TenantId").CurrentValue == null).ToList();
102106

@@ -120,7 +124,7 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
120124
var deletedMultiTenantEntities = changedMultiTenantEntities.
121125
Where(e => e.State == EntityState.Deleted).ToList();
122126

123-
// handle Tenant Id mismatches for deleted entities
127+
// handle Tenant ID mismatches for deleted entities
124128
var mismatchedDeleted = deletedMultiTenantEntities.
125129
Where(e => (string?)e.Property("TenantId").CurrentValue != null &&
126130
(string?)e.Property("TenantId").CurrentValue != tenantInfo.Id).ToList();

0 commit comments

Comments
 (0)
Please sign in to comment.