Mansur Otabekov
Mansur Otabekov

Reputation: 80

How to configure NHibernate SessionFactory in code

I'm new to NHibernate and I'm trying to configure it based on the book 'Learning NHibernate 4'. However, I'm stuck with how to configure it. I have a class called Connection but when I try to use it, NHibernate tells me it can't find 'HbmMapping'.

class Connection
    {
        public Connection() 
        {
            var cfg = new Configuration();
            cfg.DataBaseIntegration(x =>
            {
                x.Dialect<PostgreSQLDialect>();
                x.Driver<NpgsqlDriver>();
                x.ConnectionString = "Server=127.0.0.1; Port=5433; User Id=smartwarehouse; Password=$smart#2018;Database=warehouse;";
                x.ConnectionReleaseMode = ConnectionReleaseMode.OnClose;
                x.LogSqlInConsole = true;
                x.LogFormattedSql = true;


            }).AddMapping(GetMappings());

        }
          // here is Hbm dosn't find from library
     private HbmMapping GetMappings() 
        {



        }
    }

enter image description here

it gives me other two options to use like here

enter image description here

Upvotes: 0

Views: 1332

Answers (1)

David Osborne
David Osborne

Reputation: 6781

This is probably a better resource for this issue. You typically tell it where your mappings are at an assembly level...

.AddFromAssemblyOf<YourEntity>();

...so that when you add/remove mappings, you don't need to change your code.

For example, my SessionProvider has a bit like this:

Config = new NHibernateConfig();
Config.Configure(); // read config default style

Fluently
     .Configure(Config)
     .Mappings(
         m => m.FluentMappings.AddFromAssemblyOf<UserMap>()
...

I don't have .hbm files as I use derivatives of ClassMap. However, as long as the type you specify in the AddFromAssemblyOf method is in the same assembly as your .hbm files, then it should work. So something like:

Fluently
    .Configure(Config)
    .Mappings(
        m => m.HbmMappings.AddFromAssemblyOf<ATypeInYourMappingAssembly>()

Upvotes: 1

Related Questions