0

Using VS2010 code first and SQL Server 2008r2 with an MVC3 webapp, the SetInitializer creates a db as expected when missing or on model changes.

The db created has the filename format database.mdf and the **logfile database.ldf (can this default "name.ldf" format be changed to "name_log.ldf"?).

When opening the database with VS2010 server explorer (double-clicking the db in solution explorer), the VS2010 creates a new second logfile formatted database_log.ldf.

I can now browse the database in VS2010 server explorer, but can no longer debug, dropcreateonmodelchange or otherwise use the db.

When trying to debug error shown is: {"One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. If this is an existing database, the file may be corrupted and should be restored from a backup.\r\nCannot open database \"database\" requested by the login. The login failed.\r\nLogin failed for user 'My-PC\Me'.\r\nLog file 'C:\Users\Me\project\App_Data\database.ldf' does not match the primary file. It may be from a different database or the log may have been rebuilt previously."} ...which is fair enough since the project is expecting the current log file to be "database.ldf".

The ASPNETDB.MDF and aspnetdb_log.ldf files/names are also created and don't have the default log file name issue, so the issue doesn't apply to ASPNETDB.

Is it possible to change the default log file name for the SetInitializer to create a "database_log.ldf" formatted log file name instead of the "database.ldf"? I've googled without success, and looked at all the settings and options I could find in VS2010. Is the format stored in a config file somewhere that can be hand edited?

AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155

3 Answers3

0

To continue using the database in VS2010, I've ended up deleting the database.ldf log file, and renaming the new database_log.ldf file to the original database.ldf name. It's not elegant or correct, but it works for development.

This doesn't address the incorrect behaviour of codefirst creating one version of the log file and server explorer creating a second differently named logfile at different stages of the development process within the same VS2010 environment.

0

I've got exactly the same problem using VS2010 Express and SServer 2012 Express. I'm trying to work through Julie Lerman's book Programming Entity Framework 2nd Edition.

Is there no better solution than this - renaming the logfiles? I've tried to rename the files, but Windows will tell me that the file is in use, even when I shutdown both VS and SSMS. A Process Explorer search for the DB tells me that the file is in use by SServer.

I've found that all I have to do is try to create a connection string using the DB Connection wizard, and the DB in SServer gets the "extra" logfile created, and I get the same message you are seeing.

Is this a bug in VS2010Ex? Are there patches available? How do we prevent VS2010 from corrupting the DB?

Here is the directory after creating the DB, but before trying anything from VS:

E:\Program Files (x86)\Microsoft SQL Server\MSSQL11.SAMPLES\MSSQL\DATA>dir programmingefdb2*
 Volume in drive E is JET20110708-1
 Volume Serial Number is EA22-1F7C

 Directory of E:\Program Files (x86)\Microsoft SQL Server\MSSQL11.SAMPLES\MSSQL\DATA

 08/09/2013 11:39 AM 10,485,760 PROGRAMMINGEFDB2_Data.mdf
 08/09/2013 11:39 AM 1,310,720 PROGRAMMINGEFDB2_Log.ldf
 2 File(s) 11,796,480 bytes
 0 Dir(s) 250,238,443,520 bytes free

Here is the directory after making the connection in the VS project but without even trying to run the update code (and without copying the DB into the project):

E:\Program Files (x86)\Microsoft SQL Server\MSSQL11.SAMPLES\MSSQL\DATA>dir programmingefdb2*
 Volume in drive E is JET20110708-1
 Volume Serial Number is EA22-1F7C

 Directory of E:\Program Files (x86)\Microsoft SQL Server\MSSQL11.SAMPLES\MSSQL\DATA

 08/09/2013 11:51 AM 10,485,760 PROGRAMMINGEFDB2_Data.mdf
08/09/2013 11:51 AM 516,096 PROGRAMMINGEFDB2_Data_log.ldf
 08/09/2013 11:39 AM 1,310,720 PROGRAMMINGEFDB2_Log.ldf
 3 File(s) 12,312,576 bytes
 0 Dir(s) 250,237,927,424 bytes free
0

To anyone reading this thread and especially to the author, I found the solution over on the MS SQL Server forum. Here is the answer:

Visual Studio corrupting SQL Server logfile