Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nHibernate and SQL Server 2012 LocalDB

Is it possible to use LocalDB databases with NHibernate? If yes, what should be installed/configured?

Currently when trying to use connection string like Data Source=(LocalDb)\v11.0;Initial Catalog=tst1;Integrated Security=SSPI when creating SessionFactory I get

System.Data.SqlClient.SqlException : A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ----> System.ComponentModel.Win32Exception : The network path was not found

However, I'm able to connect to (LocalDb)\v11.0 via SQL Server Object Explorer, and Entity Framework works with that connection string. So, what am I doing wrong with NH, or it's impossible to use LocalDB with NH at the moment?

like image 995
Shaddix Avatar asked Jul 02 '12 17:07

Shaddix


3 Answers

You might have to use the archaic syntax with nH, e.g.:

np:\\.\pipe\LOCALDB#ABB78D50\tsql\query

(For some background on why this syntax is sometimes required for connection strings, see this tip on mssqltips.com for SQL Server 2012, and this tip for more modern versions.)

I realize this isn't an ideal answer, but until nHibernate updates to officially support SqlLocalDb, it might be your only choice... hopefully I'm wrong and someone has figured out a more elegant way to connect.

like image 120
Aaron Bertrand Avatar answered Sep 19 '22 23:09

Aaron Bertrand


You don't need to use np:\ as this type of connection string is a big headache for all. The address of the localdb named pipe changes frequently so you have to reflect that on your connection string.

To use NHibernate with localDB, get the latest version of NHibernate and use the following connection string:

Server=(localdb)\v11.0;Initial Catalog=<dbname>;User ID=<user>;password=<pwd>;Integrated Security=false;AttachDBFilename=<dbfile_path><dbfilename>.mdf

The trick is the AttachDBFilename.

like image 29
Pedro Soares Avatar answered Sep 21 '22 23:09

Pedro Soares


Yes, it is possible to connect. Go to View -> Server Explorer -> expand Data Connections then right click on your and go to Properties. You will see the areas Identity, Conenction and Misc. In Connection area you will find the Connection String needed for .xml file where NHibernate is configured. Copy that string under the connection.string property like :

<property name="connection.connection_string">Data Source=(LocalDb)\v11.0;AttachDbFilename={path}\aspnet-{projectName}-20141201132517.mdf;Initial Catalog=aspnet-{projectName}-20141201132517;Integrated Security=True</property>

I've added NHibernate to my MVC project following this post.

Hope this help.

like image 26
bogdan.rusu Avatar answered Sep 21 '22 23:09

bogdan.rusu