sql-serverfluent-nhibernateisession

How to create session with SQL Server in fluent hibernate


I'm getting exception while creating connection with SQL Server.

ISession factory is shown below from which I'm getting exception:

private static ISessionFactory CreateSessionFactory()
{
   return Fluently.Configure()
            .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
            .ConnectionString(c => c.FromConnectionStringWithKey("FNHConnection"))
            )
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<SessionProvider>())
            .BuildSessionFactory();

    //// code added to configure the database connection .. 
    //var FNHConfig = MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("FNHConnection"));
    //PersistenceModel persistenceModel = new PersistenceModel();
    //persistenceModel.AddMappingsFromAssembly(Assembly.GetCallingAssembly());
    //_config.AddProperties(FNHConfig.ToProperties());
    //persistenceModel.Configure(_config);
    ////return _config.BuildSessionFactory();

    //return Config.BuildSessionFactory();
}

The following exception is thrown:

Locating source for 'd:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs'. (No checksum.)
The file 'd:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs' does not exist.
Looking in script documents for 'd:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs'...
Looking in the projects for 'd:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs'.
The file was not found in a project.
Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\'...
Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\mfc\'...
Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\atl\'...
Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\'...
The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file:
d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs.
The debugger could not locate the source file 'd:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs'.


Solution

  • You're missing BuildConfiguration()

     .Mappings(m =>
                m.FluentMappings.AddFromAssemblyOf<SessionProvider>())
     .BuildConfiguration()
     .BuildSessionFactory();