How to set custom DriverConnectionProvider with Fl

2019-05-04 12:46发布

问题:

How to set custom DriverConnectionProvider with Fluent NHibernate?

Best regards, Alexey Zakharov

回答1:

I find solution. Here is the small sample, which how it could be done.

Fluently.Configure()
    .Database(MsSqlConfiguration.MsSql2008
                    .ConnectionString(".......")
                    .ShowSql()
                    .Provider<TenantConnectionProvider>()
                    )

public class TenantConnectionProvider : DriverConnectionProvider
{
    public override IDbConnection GetConnection()
    {
        IDbConnection conn = Driver.CreateConnection();
        try
        {
            conn.ConnectionString = // Tenant connection string provider called here
            conn.Open();
        }
        catch (Exception)
        {
            conn.Dispose();
            throw;
        }

        return conn;


   }
}