I have this error pop up in the last couple days. I was following Asp.Net Mvc 4 and the Web Api: Building a REST Service from Start to Finish on how to build a Rest Service API then along the way, came this:
Cannot open database "ServicesDb" requested by the login. The login failed.
Login failed for user 'my-PC\my'.
So I stopped for a while and read the book and nothing. So I downloaded the book companion project and when I run it, I encounter the same error as with my own service.
This is a shock for me, as I have always used Windows Authentication
for local database engine. I have had no problems when I have used Entity Framework, so I am beginning to think that this has something to do with NHibernate and Fluent Nhibernate which I am using for the first time.
Connection string:
<connectionStrings>
<add name="ServicesDb"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDb)\v11.0;
<!-- connectionString="Server=(LocalDb)\v11.0; <----- Tried this too -->
initial catalog=ServicesDb;
Integrated Security=True;
Application Name=Services API Website" />
</connectionStrings>
Has anyone else ran into this problem while following this book? Does anyone know what could be causing this, and how I can get passed it?
UPDATE: Here is the code that fails to execute, the book uses Ninject and so am i.
private void ConfigureNHibernate(IKernel container)
{
// Build the NHibernate ISessionFactory object
var sessionFactory = FluentNHibernate //Fails here, sessionFactory variable not instantiated.
.Cfg.Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2008.ConnectionString(
c => c.FromConnectionStringWithKey("ServicesDb")))
.CurrentSessionContext("web")
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SqlCommandFactory>())
.BuildSessionFactory();
// Add the ISessionFactory instance to the container
container.Bind<ISessionFactory>().ToConstant(sessionFactory); //sessionFactory needed here
// Configure a resolver method to be used for creating ISession objects
container.Bind<ISession>().ToMethod(CreateSession);
container.Bind<ICurrentSessionContextAdapter>().To<CurrentSessionContextAdapter>();
}
Okay, in both cases i was assuming that the database was there. This is because in Visual Studio, the connection string throws a shadow of the application's database in Server Explorer. And because of that and the fact that in the past i have worked with Entity Framework Code First, which uses Migration and took care of all my SQL stuff for me.
I opened Microsoft SQL Server Management Studio and confirm that the database was indeed there. When i realised that it was there, i created it and made sure that it matched my connection string. After that all was good and i could execute my SQL scripts within Visual Studio.