I've created a simple context using a code-first approach with Entity Framework using the below connection string. All goes well and I find myself with a .mdf
and .ldf
file after running a test method.
"data source=.\SQLEXPRESS;Integrated Security=SSPI;database=testdb;AttachDBFilename=Q:\Temp\testdb.mdf;User Instance=true"
For some reason I wish to delete this database. I try to look for the database in SSMS, but I can't find it. It is not registered under my databases. Now I feel I have 3 options:
Attach the database in SSMS and then delete it
Connect to the database in VS2015 Server Explorer by searching for the .MDF
file and then deleting the database from VS.
Add database via Server Explorer
Press to browse in SQL Server Object Explorer
Appears in Server Object Explorer where I can press delete
Just delete the .mdf
and .ldf
files
All three methods remove the .mdf
and .ldf
files.
HOWEVER : when I start up the context again, I expect it to create the files again from scratch. however I get the following error:
An exception occurred while initializing the database. See the InnerException for details.
The underlying provider failed on Open.
Cannot open database "testdb" requested by the login.The login failed.
Login failed for user 'DESKTOP-XXXXXXX\\XXXXX'.
I feel I've deleted every reference to this database. I cannot find any other references. Yet somehow it appears there is one because it is trying to login rather than creating a new database.
Question: does anyone know how to address this issue? How can I properly delete the database + reference?
UPDATE :
In VS open connections during my test before deleting the file I see the following connection string.
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=Q:\Temp\testdb.mdf;Integrated Security=True;Connect Timeout=30
However when exploring my Localdb in VS2015 there are no databases listed.
Can you try explicit initialization of your Db with with Db initialization strategy CreateDatabaseIfNotExists
. Something along the lines of ..
public class YourDBContext: DbContext
{
public YourDBContext(): base("YourDBContextConnStr")
{
Database.SetInitializer<YourDBContext>(new CreateDatabaseIfNotExists<YourDBContext>());
}
}
Cheers
Being triggered by the fact that I initialized using ".\\SQLEXPRESS" and EF changed it to "(LocalDB)\\MSSQLLocalDB" I've changed the connectionstring.
"data source=(LocalDB)\MSSQLLocalDB;Integrated Security=SSPI;database=testdb;AttachDBFilename=Q:\Temp\testdb.mdf");
Et voila. Using the SQL Express LocalDB in my connectionstring as a datasource I can now mindlessly delete and recreate databases. Further more the databases now correctly show up in my SQL Server Object Explorer under LocalDb (instead of not showing up anywhere at all).
I still dont know why it could initially create with the ".\\SQLEXPRESS" connectionstring but not re-create. As if it was somewhere hiding the information that the .mdf database exists and was always trying to reconnect to it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.