Skip to content

Instantly share code, notes, and snippets.

@merken
Created September 25, 2019 19:44
Show Gist options
  • Save merken/cf91c0bb28544eee2af2decbebe12f0b to your computer and use it in GitHub Desktop.
Save merken/cf91c0bb28544eee2af2decbebe12f0b to your computer and use it in GitHub Desktop.
DatabaseInterceptor
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