Dinny
Dinny

Reputation: 475

Fluent NHibernate Database First Configuration

I am new to Fluent Nhibernate. We have started using it for a Back office application for Data Access.

The client already has a Database with them so we would like to use the Database first approach as we don't want to generate tables.

For a Proof of concept solution, I used the below Session Factory configuration

var oracleConfiguration = OracleDataClientConfiguration.Oracle10.ConnectionString("Data Source=MyDB;User Id=MyUserName; Password=myPassword; Pooling=true");
        return Fluently.Configure()
                       .Database(oracleConfiguration)
                       .Mappings(m => m.FluentMappings.Add<NhtestMap>())
                       .ExposeConfiguration((config) => { new SchemaExport(config).Create(false, true); })
                       .BuildSessionFactory();

Since I already have this table in my DB before running this code, the method 'ExposeConfiguration' has dropped the test table 'NHTest'.

I read few blogs and came to know that this method creates table on first run (Which is the required behavior for Code First approach). But for us, with the DB First approach, we don't want to drop or create tables.

Can some one please help me out to correctly configure the Session Factory for DB First approach ?

Upvotes: 1

Views: 711

Answers (1)

Dai Bok
Dai Bok

Reputation: 3606

Try this:

var oracleConfiguration = OracleDataClientConfiguration.Oracle10.ConnectionString("Data Source=MyDB;User Id=MyUserName; Password=myPassword; Pooling=true");
        return Fluently.Configure()
                       .Database(oracleConfiguration)
                       .Mappings(m => m.FluentMappings.Add<NhtestMap>())
//  Remove this line //.ExposeConfiguration((config) => { new SchemaExport(config).Create(false, true); })
                       .BuildSessionFactory();

Upvotes: 2

Related Questions