2
2
// Refer to the solution LICENSE file for more information.
3
3
4
4
using System . Linq ;
5
+ using Finbuckle . MultiTenant . Abstractions ;
5
6
using Finbuckle . MultiTenant . EntityFrameworkCore ;
6
7
using Microsoft . EntityFrameworkCore ;
7
8
8
9
// ReSharper disable once CheckNamespace
9
10
namespace Finbuckle . MultiTenant ;
10
11
11
- public static class IMultiTenantDbContextExtensions
12
+ public static class MultiTenantDbContextExtensions
12
13
{
13
14
/// <summary>
14
15
/// Checks the TenantId on entities taking into account
@@ -17,7 +18,7 @@ public static class IMultiTenantDbContextExtensions
17
18
public static void EnforceMultiTenant < TContext > ( this TContext context ) where TContext : DbContext , IMultiTenantDbContext
18
19
{
19
20
var changeTracker = context . ChangeTracker ;
20
- var tenantInfo = context . TenantInfo ;
21
+ ITenantInfo tenantInfo = context . TenantInfo ! ;
21
22
var tenantMismatchMode = context . TenantMismatchMode ;
22
23
var tenantNotSetMode = context . TenantNotSetMode ;
23
24
@@ -26,14 +27,17 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
26
27
Where ( e => e . Metadata . IsMultiTenant ( ) ) . ToList ( ) ;
27
28
28
29
// 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
+ }
31
35
32
36
// get list of all added entities with MultiTenant annotation
33
37
var addedMultiTenantEntities = changedMultiTenantEntities .
34
38
Where ( e => e . State == EntityState . Added ) . ToList ( ) ;
35
39
36
- // handle Tenant Id mismatches for added entities
40
+ // handle Tenant ID mismatches for added entities
37
41
var mismatchedAdded = addedMultiTenantEntities .
38
42
Where ( e => ( string ? ) e . Property ( "TenantId" ) . CurrentValue != null &&
39
43
( string ? ) e . Property ( "TenantId" ) . CurrentValue != tenantInfo . Id ) . ToList ( ) ;
@@ -71,7 +75,7 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
71
75
var modifiedMultiTenantEntities = changedMultiTenantEntities .
72
76
Where ( e => e . State == EntityState . Modified ) . ToList ( ) ;
73
77
74
- // handle Tenant Id mismatches for modified entities
78
+ // handle Tenant ID mismatches for modified entities
75
79
var mismatchedModified = modifiedMultiTenantEntities .
76
80
Where ( e => ( string ? ) e . Property ( "TenantId" ) . CurrentValue != null &&
77
81
( string ? ) e . Property ( "TenantId" ) . CurrentValue != tenantInfo . Id ) . ToList ( ) ;
@@ -96,7 +100,7 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
96
100
}
97
101
}
98
102
99
- // handle Tenant Id not set for modified entities
103
+ // handle Tenant ID not set for modified entities
100
104
var notSetModified = modifiedMultiTenantEntities .
101
105
Where ( e => ( string ? ) e . Property ( "TenantId" ) . CurrentValue == null ) . ToList ( ) ;
102
106
@@ -120,7 +124,7 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
120
124
var deletedMultiTenantEntities = changedMultiTenantEntities .
121
125
Where ( e => e . State == EntityState . Deleted ) . ToList ( ) ;
122
126
123
- // handle Tenant Id mismatches for deleted entities
127
+ // handle Tenant ID mismatches for deleted entities
124
128
var mismatchedDeleted = deletedMultiTenantEntities .
125
129
Where ( e => ( string ? ) e . Property ( "TenantId" ) . CurrentValue != null &&
126
130
( string ? ) e . Property ( "TenantId" ) . CurrentValue != tenantInfo . Id ) . ToList ( ) ;
0 commit comments