5

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?

kndroid
  • 33
  • 8
mrt181
  • 5,080
  • 8
  • 66
  • 86

2 Answers2

9

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...
Florian Lim
  • 5,332
  • 2
  • 27
  • 28
  • Figured it was something like this but wasn't sure how easy it would be. Strange that it isn't build-in like the other common databases. – Sixto Saez Apr 15 '11 at 14:18
  • @Florian Lim can this map the table bcoz im using DentrixSQL and can only be manipulated through the used of SDK API with obdc – Blaire Andaloc Nov 25 '15 at 11:53
1

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.

Sixto Saez
  • 12,610
  • 5
  • 43
  • 51