2

I'm using Brock Allen's awesome MembershipReboot library to provide identity services in my .NET application. Per Getting Started with MembershipReboot, MembershipReboot uses Entity Framework (EF) for it's data access.

The sample apps contain a SQL Server database MembershipReboot.mdf. From this database I exported the schema and data and imported it into my application's database (MyTestApp.mdf - see below).

You can see where MembershipReboot.mdf comes from as the samples that accompany MembershipReboot define the following connection string::

<add name="MembershipReboot" connectionString="Data Source=(LocalDb)\bla;Initial Catalog=MembershipReboot;Integrated Security=True" providerName="System.Data.SqlClient"/>

I set the following connection string for my app's database:

<add name="DefaultConnectionString" connectionString="Server=(LocalDb)\bla;AttachDbFilename=|DataDirectory|\MyTestApp.mdf;Initial Catalog=MyTestApp;Integrated Security=True" providerName="System.Data.SqlClient" />

Until recently, all was well. Then, upon one launch of my application I started receiving - intermittently - the following error upon first database access:

Cannot attach the file 'C:\Users\mkrieger\Google Drive\Projects\MyTestApp\MyTestApp\App_Data\MembershipReboot.mdf' as database 'MembershipReboot'.

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 'C:\Users\mkrieger\Google Drive\Projects\MyTestApp\MyTestApp\App_Data\MembershipReboot.mdf' as database 'MembershipReboot'.

I did a full text search of my application's directory followed by a Strings search of the executables and all binary files for "MembershipReboot.mdf" but came up empty.

What am I missing?

Community
  • 1
  • 1
Matthew
  • 947
  • 2
  • 13
  • 20
  • 1
    Do you have a `` at the top of your connection strings in your config file? – Basic Sep 15 '14 at 17:43
  • @Basic - I didn't but I just added and am trying. But if removes references to inherited connection strings shouldn't I have found the culprit connection string during my text search? – Matthew Sep 15 '14 at 17:52
  • It depends... There are a number of ways strings could be specified and being unfamiliar with the library in question, this is the easiest thing to test. Possible reasons it might not be found include storage in an external file/the registry, filename being built dynamically (`String.Format("{0}.{1}", "MembershipReboot", extension)`), etc... again, not saying they're likely, just ruling out possibilities – Basic Sep 15 '14 at 17:57
  • @Basic - One definite thing is that I used to have a "MembershipReboot" connection string entry which pointed to MembershipReboot.mdf. So maybe it's cached somewhere. – Matthew Sep 15 '14 at 18:39
  • @Basic - No luck. FYI I am using LocalDb here. When I disable LocalDb (for example by renaming the .exe) the app fails indicating it cannot connect to the LocalDb instance. – Matthew Sep 15 '14 at 21:07
  • I am facing a similar situation where I am not mentioning LocalDb in any of my connection strings yet the application still tries to attach the mdf file when I do a Update-Database/Add-Migration. Hopefully someone comes out with a solution. – ahmed.eltawil Oct 21 '14 at 14:03
  • @AlbertEltawil - I am really glad you posted your comment because it reminded me that I contacted Brock and got the answer from him, which I am posting now. – Matthew Oct 27 '14 at 18:06

1 Answers1

1

Brock Allen has seen this problem before (the problem has nothing whatsoever to do with MembershipReboot or any particular library).

Yes, I’ve seen this error before. It’s when you delete the sql express files (mdf, etc) but don’t delete the DB from the DB server – in other words, you need to delete the database from within Sql Server Mgmt Studio or from the Sql Server explorer in Visual Studio.

This did the trick for me.

Matthew
  • 947
  • 2
  • 13
  • 20