开发者

Fluent nHibernate Database Connection

In my application i m using fluent nhibernate with MVC 3. i have separate business layer , DAL , and repository layer, now i have to ask where to create the database connection , weather in the DAL or in the MVC (user interface layer)

Note: i want to define the connection string in web.config file.

Does anyone kno开发者_StackOverfloww ?

Thanks


I do something like this inside my MVC project:-

  <connectionStrings>
    <add name="db" 
      connectionString="Server=.;Database=NHCookbook;Trusted_Connection=SSPI;"/>
  </connectionStrings>

Then in the mvc project I build my session factory like:-

  var nhConfig = Fluently
    .Configure()
    .Database(MsSqlConfiguration.MsSql2008
                .ConnectionString(c => c.FromConnectionStringWithKey("db"))
    )
    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<AMODELCLASS>()
    )
    .BuildConfiguration();

   sessionFactory = nhConfig.BuildSessionFactory();


Since the application running your code will be the ASP.NET MVC project, the configuration settings in Web.config will be available in any class library. Thus, you should be able to use the configuration code Rippo suggested in any project (however, it won't be reusable from other applications if they don't define a connection string with the same key).

I suggest you let the configuration code for NHIbernate reside in the DAL project, along with other NHibernate related things.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜