Is it possible to use (fluent)nhibernate with an odbc connection?

♀尐吖头ヾ 提交于 2019-12-05 12:30:10

You can create your own OdbcConfiguration class that derives from PersistenceConfiguration.

Depending on your database, you will have to replace the Dialect in the following class.

public class OdbcConfiguration : 
    PersistenceConfiguration<OdbcConfiguration, 
        FluentNHibernate.Cfg.Db.OdbcConnectionStringBuilder>
{
    protected OdbcConfiguration()
    {
        Driver<NHibernate.Driver.OdbcDriver>();
    }

    public static OdbcConfiguration MyDialect // <-- insert any name here
    {
        get
        {
            // insert the dialect you want to use
            return new OdbcConfiguration().Dialect<NHibernate.Dialect.MyDialect>();
        }
    }
} 

Then, in Fluent NHibernate, use that OdbcConfiguration:

// replace MyDialect here, too
Fluently.Configure()
    .Database(OdbcConfiguration.MyDialect.ConnectionString("DSN=...;UID=...;PWD=...")
            .Driver<NHibernate.Driver.OdbcDriver>()
            .Dialect<NHibernate.Dialect.MyDialect>() // <-- again, change this
            .etc...

Didn't find any sample code using OdbcConnectionStringBuilder after a quick search. Fluent NHibernate doesn't seem to have a corresponding "OdbcConfiguration" object to use with the OdbcConnectionStringBuilder. If you don't use Fluent NHibernate for configuring the database (you can still use Fluent for all the object mapping though), you can configure via the hibernate.cfg.xml file, look at the DB2 example config for using ODBC provider.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!