Cannot find the OdeToFoodDb.mdf' as database 'OdeToFoodDb'

May 7, 2013 at 6:52 PM
Edited May 7, 2013 at 6:54 PM
Hello

I have already downloaded the code fro MVC4, the latest version(**), and the first time I run it, it did it alright.
I got a problem with Nuget and to solve it I decided to delete the folder with OdeToFood.
Then I unzipped it again as I did before but now, it doesn´t run.
A yellow page with a message about the it cannot find the database under App_Data.
Weird cos the first time it was create and was working.
(**)By the way, I downloaded a version of FEB 08, 2013.

Here's the message:

Server Error in '/' Application.

Cannot attach the file 'D:\PROJECTS\VISUAL STUDIO work\K.ScottAllen\OdeToFood\odetofoodmvc4-(Feb 08, 2013)\OdeToFood\App_Data\OdeToFoodDb.mdf' as database 'OdeToFoodDb'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Cannot attach the file 'D:\PROJECTS\VISUAL STUDIO work\K.ScottAllen\OdeToFood\odetofoodmvc4-(Feb 08, 2013)\OdeToFood\App_Data\OdeToFoodDb.mdf' as database 'OdeToFoodDb'.

Source Error:



Line 22:
Line 23: var migrator = new DbMigrator(new Configuration());
Line 24: migrator.Update();
Line 25:
Line 26: if (!WebSecurity.Initialized)


Source File: d:\PROJECTS\VISUAL STUDIO work\K.ScottAllen\OdeToFood\odetofoodmvc4-(Feb 08, 2013)\OdeToFood\Global.asax.cs Line: 24

Stack Trace:



[SqlException (0x80131904): Cannot attach the file 'D:\PROJECTS\VISUAL STUDIO work\K.ScottAllen\OdeToFood\odetofoodmvc4-(Feb 08, 2013)\OdeToFood\App_Data\OdeToFoodDb.mdf' as database 'OdeToFoodDb'.]
System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +414
System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +78
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +167
System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) +143
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1 retry) +83
System.Data.SqlClient.SqlConnection.Open() +96
System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable1 migrationStatements) +91
System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable
1 migrationStatements) +42
System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable1 operations, Boolean downgrading, Boolean auto) +1025
System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration) +494
System.Data.Entity.Migrations.Infrastructure.MigratorBase.ApplyMigration(DbMigration migration, DbMigration lastMigration) +56
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable
1 pendingMigrations, String targetMigrationId, String lastMigrationId) +186
System.Data.Entity.Migrations.Infrastructure.MigratorBase.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) +59
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +896
System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update() +33
OdeToFood.MvcApplication.Application_Start() in d:\PROJECTS\VISUAL STUDIO work\K.ScottAllen\OdeToFood\odetofoodmvc4-(Feb 08, 2013)\OdeToFood\Global.asax.cs:24

[HttpException (0x80004005): Cannot attach the file 'D:\PROJECTS\VISUAL STUDIO work\K.ScottAllen\OdeToFood\odetofoodmvc4-(Feb 08, 2013)\OdeToFood\App_Data\OdeToFoodDb.mdf' as database 'OdeToFoodDb'.]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9859441
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): Cannot attach the file 'D:\PROJECTS\VISUAL STUDIO work\K.ScottAllen\OdeToFood\odetofoodmvc4-(Feb 08, 2013)\OdeToFood\App_Data\OdeToFoodDb.mdf' as database 'OdeToFoodDb'.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9873784
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101

System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18034
May 8, 2013 at 12:50 AM
Edited May 8, 2013 at 12:51 AM
I already have found what was happening. Everything is fixed-up now. I m gonna tell you later what it was.
May 31, 2013 at 8:01 AM
hjoab wrote:
I already have found what was happening. Everything is fixed-up now. I m gonna tell you later what it was.
You have you forgotten to provide the details that could help others?
May 31, 2013 at 6:38 PM
Edited May 31, 2013 at 6:44 PM
Ok yes sorry. As I remember I was having the same problems, and it looks like the reason was that I have already installed SQL Express. I looks that the first time when you compile the solution, VS doesn´t recognize the presence of the SQL Server express, but after calling database migrations the first time it becomes aware of it and does not generate the DB again under App_Data. (WHY?? I don't know but I could guess that the logic behind is that if you already have the SQL Server express installed in your machine, why to use LocalBD instead). So what I did was to change the string connection inside the web config for connecting to the SQL Server Express into previously create OdeToFoodDB and everything was working fine.
Jun 1, 2013 at 9:38 AM
Thanks LOADS..S..S..S..S.S.S.S.S... hjoab. this solved it for me as well. Sometimes I wish there would have been an easy way to back out 'Database migrations' and just regen from that point forward. Maybe there is and I just haven't read the right post . . . but this sure got me out of a jam!!!

Thanks again . . .
tjdarth1
Jun 1, 2013 at 4:34 PM
yjdarth1 you're welcome, and yes I agree with you, but in the hundred thousands maybe millions of lines in tools like VS and associated ones we can expect some pitfalls.
Oct 1, 2013 at 4:46 PM
Using SQL 2008 Express and VS 2012 Express, I had to change the connection string in the webConfig file to read:

[add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=OdeToFoodDb;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\OdeToFoodDb.mdf" ...

to get the database portion to launch.