Created
September 25, 2019 19:44
-
-
Save merken/cf91c0bb28544eee2af2decbebe12f0b to your computer and use it in GitHub Desktop.
DatabaseInterceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Data.Common; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.EntityFrameworkCore.Diagnostics; | |
namespace efcore_tenancy.Infrastructure | |
{ | |
public class DatabaseInterceptor : DbCommandInterceptor | |
{ | |
private readonly TenantInfo tenantInfo; | |
public DatabaseInterceptor(TenantInfo tenantInfo) | |
{ | |
this.tenantInfo = tenantInfo; | |
} | |
public override Task<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, | |
CommandEventData eventData, | |
InterceptionResult<DbDataReader> result, | |
CancellationToken cancellationToken = default) | |
{ | |
command.CommandText = $"USE {tenantInfo.Name}DB {command.CommandText}"; | |
return base.ReaderExecutingAsync(command, eventData, result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment