如何使用Fluent NHibernate设置自定义DriverConnectionProvider

时间:2010-05-28 09:21:14

标签: nhibernate fluent-nhibernate

如何使用Fluent NHibernate设置自定义DriverConnectionProvider?

祝你好运, Alexey Zakharov

1 个答案:

答案 0 :(得分:13)

我找到解决方案。这是一个小样本,它是如何完成的。

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;


   }
}