Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 467
mrt181 Avatar asked Jan 20 '23 00:01

mrt181


2 Answers

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...
like image 163
Florian Lim Avatar answered May 03 '23 07:05

Florian Lim


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.

like image 29
Sixto Saez Avatar answered May 03 '23 05:05

Sixto Saez