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

旧时模样 提交于 2019-12-22 08:34:51

问题


i have to use a custom odbc driver.

All i need to pass as a connection string is the DSN.

How do i do this with (fluent)nhibernate? FluentNHibernate.Cfg.Db does only offer a OdbcConnectionStringBuilder class with an DSN method. How do i use this?


回答1:


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...



回答2:


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.



来源:https://stackoverflow.com/questions/5676812/is-it-possible-to-use-fluentnhibernate-with-an-odbc-connection

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