You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Make sure to dispose of the database context instance when it is no longer needed, or better yet use a `using` block or
345
+
variable. This method will work for any database context class expecting a `IMultiTenantContextAccessor` in its
346
+
constructor and an options DbContextOptions<T> in its constructor.
305
347
306
348
## Design Time Instantiation
307
349
308
350
Given that a multi-tenant database context usually requires a tenant to function, design time instantiation can be
309
351
challenging. By default, for things like migrations and command line tools Entity Framework core attempts to create an
310
352
instance of the context using dependency injection, however usually no valid tenant exists in these cases and DI fails.
311
-
For this reason it is recommended to use a [design time factory](https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation#from-a-design-time-factory) wherein a dummy `ITenantInfo` is
312
-
constructed with the desired connection string and passed to the database context constructor.
313
-
314
-
## Registering with ASP.NET Core
315
-
316
-
When registering the database context as a service in ASP.NET Core it is important to take into account whether the
317
-
connection string and/or provider will vary per-tenant. If so, it is recommended to set the connection string and
318
-
provider in the `OnConfiguring` database context method as described above rather than in the `AddDbContext` service
319
-
registration method.
353
+
For this reason it is recommended to use
354
+
a [design time factory](https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation#from-a-design-time-factory)
355
+
wherein a dummy `ITenantInfo` with the desired connection string and passed to the database context creation factory
356
+
described above.
320
357
321
358
## Adding Data
322
359
323
-
Added entities are automatically associated with the current `TenantInfo`. If an entity is associated with a
324
-
different `TenantInfo` then a `MultiTenantException` is thrown in `SaveChanges` or `SaveChangesAsync`.
360
+
Added entities are automatically associated with the current `TenantInfo`. If an entity is associated with a different
361
+
`TenantInfo` then a `MultiTenantException` is thrown in `SaveChanges` or `SaveChangesAsync`.
325
362
326
363
```csharp
327
364
// Add a blog for a tenant.
@@ -417,8 +454,8 @@ property on the database context:
417
454
418
455
*`TenantMismatchMode.Throw` - A `MultiTenantException` is thrown (default).
419
456
*`TenantMismatchMode.Ignore` - The entity is added or updated without modifying its `TenantId`.
420
-
*`TenantMismatchMode.Overwrite` - The entity's `TenantId` is overwritten to match the database context's
421
-
current `TenantInfo`.
457
+
*`TenantMismatchMode.Overwrite` - The entity's `TenantId` is overwritten to match the database context's current
458
+
`TenantInfo`.
422
459
423
460
## Tenant Not Set Mode
424
461
@@ -428,5 +465,5 @@ or `SaveChangesAsync`. This behavior can be changed by setting the `TenantNotSet
428
465
429
466
*`TenantNotSetMode.Throw` - For added entities the null `TenantId` will be overwritten to match the database context's
430
467
current `TenantInfo`. For updated entities a `MultiTenantException` is thrown (default).
431
-
*`TenantNotSetMode.Overwrite` - The entity's `TenantId` is overwritten to match the database context's
432
-
current `TenantInfo`.
468
+
*`TenantNotSetMode.Overwrite` - The entity's `TenantId` is overwritten to match the database context's current
0 commit comments