Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't recreate SQL Server Express database with Entity Framework Code First and MVC 3 without changing the database name

When I run the application with the following connection string the database file is created successfully.

<add name="ConnString1"
     connectionString="Data Source=.\SQLEXPRESS;
                       Database=Database1;
                       Integrated Security=SSPI;
                       AttachDBFilename=|DataDirectory|aspnetdb.mdf;
                       User Instance=true"
     providerName="System.Data.SqlClient" />

If I delete the database file and try to run the application again the database file fails to be created and I get the following inner exceptions:

The underlying provider failed on Open.
{"Cannot open database \"Database1\" requested by the login. The login failed.\Database1\nLogin failed for user 'computer\\someuser'."}

If I change Database=Database1 to Database=Database2 in the connection string then the database file is created successfully. The problem repeats itself always.

How can I recreate the database file without having to change the database name?

like image 429
adolfojp Avatar asked Nov 14 '22 04:11

adolfojp


1 Answers

Check to make sure the directory rights allow you to delete the mdf file and the the login has drop schema/table privileges directory rights are a common issue with mdf files due the high security placed on these files due to their potentially sensitive nature

like image 136
Chris McGrath Avatar answered Jan 27 '23 02:01

Chris McGrath