Cannot log in to SQL Server using NHibernate, but works with Management Studio or sqlcmd

Error getting error while trying to connect to my database using NHibernate:

var config = Fluently.Configure() .Database(MsSqlConfiguration .MsSql2005 .ConnectionString("Data Source=SERVER1\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=xxx")) .Mappings(x => x.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())); 

With Error 18456 State 5 in the log viewer in SQL Management Studio.

But using sqlcmd or Management Studio, it works fine (using Windows Auth)!

What could be the problem?

EDIT NHibernate Exception:

 Test method ProjektLogg.Tests.NHibernateTests.ShouldBeAbleToGenerateFactory threw exception: FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail. ---> NHibernate.HibernateException: Login failed for user XXX-INTRANET\username. ---> System.Data.SqlClient.SqlException: Login failed for user XXX-INTRANET\username. 
+4
source share
4 answers

This error message means "Invalid UserID". If you use IIS, make sure that your application pool uses the same identifier as the user with whom you are connecting to the database in the management studio.

If you are not working in IIS, make sure that your current user account (Windows) has access to the database because you are using Integrated Security.

Error message link: http://blogs.msdn.com/b/sql_protocols/archive/2006/02/21/536201.aspx

EDIT: Wait, you are missing the start directory . Try the following:

 "Data Source=SERVER1\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=YourDatabase" 
+2
source

I cleared the whole solution, now it works. Sorry to waste your time guys. Appreciate your answers.

0
source

I had a similar problem, and I realized that, unlike Entity Framework (Fluent), NHibernate does not create a database for you. The solution was to create an empty database.

The following segment in ConnectionString means that the xxx database must already exist:

 Initial Catalog=xxx 
0
source

I could not get it to work with Integrated Security, I had to provide the user and password in the connection string.

0
source

All Articles