Not necessarily that will be an answer, but maybe it will help!
Settings
Some questions about your settings:
How are you configuring the NHibernate session?
Are you using Schema? SchemaUpdate
? SchemaExport
?
Is NHibernate creating your tables?
Basically these settings will tell if NHibernate will compare the metadata of your tables with your Mapping, and whether you will create some DDL Definition Language) in the database (This impacts directly on the first connection).
I can demonstrate 3 configuration scenarios to compare with your settings:
-
new SchemaExport(cfg).Create(false, true); // apagar o database e criar novamente todas as tabelas
(recommended only to use in tests, since the data / tables will be erased with each new connection, NEVER in production );
-
new SchemaUpdate(cfg).Execute(false, true); // atualizar o database, acrescentando novas tabelas e colunas sem excluir o que não está no mapeamento
(recommended for use in tests, and in the development phase while modeling tables, NEVER in production , as it will compare the metadata of the database with the mapping and add the DDLs that are not in the database);
-
new SchemaExport(cfg).Create(false, false); // normal, não criar nenhum DDL no database
(Recommended, for any development phase, if you do not want NHibernate to change its database structure, and this setting does not make metadata comparison and does not create any DDL when creating the connection, MUST be used in production , because it is the least configuration overflow at startup).
About initializing NHibernate at system startup!
You can initialize the Nhibernate connection at any time by running any routine in the database (query, insert, etc.).
You can simply on the Home Controller make a simple query to initialize NHibernate. Or you can do this at system startup, such as Application_Start()
and Globa.asax.cs
in the case of ASP.NET < 5, or Startup.cs
in ASP.NET 5.