zeroframework/Services/DeviceCenter/ZeroFramework.DeviceCenter.Infrastructure/ConnectionStrings/TenantDbConnectionInterceptor.cs
2023-12-05 17:22:48 +08:00

23 lines
1.1 KiB
C#

using Microsoft.EntityFrameworkCore.Diagnostics;
using System.Data.Common;
namespace ZeroFramework.DeviceCenter.Infrastructure.ConnectionStrings
{
public class TenantDbConnectionInterceptor(IConnectionStringProvider connectionStringProvider) : DbConnectionInterceptor
{
private readonly IConnectionStringProvider _connectionStringProvider = connectionStringProvider;
public override InterceptionResult ConnectionOpening(DbConnection connection, ConnectionEventData eventData, InterceptionResult result)
{
connection.ConnectionString = _connectionStringProvider.GetAsync().Result;
return base.ConnectionOpening(connection, eventData, result);
}
public override async ValueTask<InterceptionResult> ConnectionOpeningAsync(DbConnection connection, ConnectionEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default)
{
connection.ConnectionString = await _connectionStringProvider.GetAsync();
return await base.ConnectionOpeningAsync(connection, eventData, result, cancellationToken);
}
}
}