Finbuckle.MultiTenant icon indicating copy to clipboard operation
Finbuckle.MultiTenant copied to clipboard

Feature Request: AddInterceptors for Shared Database

Open VictorioBerra opened this issue 2 years ago • 2 comments

Add support for https://docs.microsoft.com/en-us/ef/core/logging-events-diagnostics/interceptors

Today, when using Data Isolation with Entity Framework Core - Shared Database, we have two options:

  1. Implement IMultiTenantDbContext and used the helper methods as described here, or
  2. Derive from MultiTenantDbContext which handles the details for you.

It would be nice for a third options:

public class MyContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.AddInterceptors(new MultiTenantInterceptor());
    }
    // ...
}

or in Startup.cs:

var options = new DbContextOptionsBuilder()
    .AddInterceptors(new MultiTenantInterceptor())
    .UseSqlServer(...)
    .Options;
using (var ctx = new MyContext(options))
{
    // ...
}

VictorioBerra avatar Feb 18 '22 00:02 VictorioBerra

Thanks for the suggestion. Seems like a good alternative method.

AndrewTriesToCode avatar Feb 19 '22 01:02 AndrewTriesToCode

Well the cool thing about it is you can register multiple, and you don't have to inherit anything or override save changes.

For example when using something like this https://github.com/thepirat000/Audit.NET/tree/master/src/Audit.EntityFramework#3-with-the-provided-save-changes-interceptor

Then I'd just have to register a couple interceptors and be done.

VictorioBerra avatar Feb 19 '22 02:02 VictorioBerra