Basically I\'ve followed a tutorial and decided to delete the .mdf
file afterwards.
Now whenever I try to run the application I get the following error
Take a look at this: Entity Framework don't create database
I would try giving the database a different name. Sometimes you can run into problems with SQL Express when trying to create a database with the same name a second time. There is a way to fix this using SQL Server Management Studio but it's generally easier to just use a different database name.
Edit This answer was accepted because it confirms the bug and the workaround used by OP (renaming database could help). I totally agree that renaming the database is not really an acceptable way, and does not totally solve the issue. Unfortunatly I didn't check the other ways to really solve it in SSMS.
Strangely, for the exact same issue, what helped me was changing the ' to 'v11.0' in the following section of the config.
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
Ran into the similar problem not exactly the same, A case of Database already existed the issue was solved by following code.
<add name="DefaultConnection" connectionString="Data Source=.;AttachDbFilename=|DataDirectory|\aspnet-EjournalParsing-20180925054839.mdf;Initial Catalog=aspnet-EjournalParsing-20180925054839;Integrated Security=True"
providerName="System.Data.SqlClient" />
I recently ran into the same problem. Here is one thing to check. in visual studio there are three place we should check and remove the database.
1. Solution Explorer's App_Data folder
2. Server Explorer's Data Connection menu
3. SQL Server Object Explorer
When I delete database from the first two point, the error still occurs. So, I needed to delete the database from the SQL Server Object Explorer as well. Then I could easily run the 'update-database' command without error. Hope this helps.
I didn't read all the responses, but if your .mdf
file is NOT part of the SQL installation path, like C:\Temp
, then the SQL Database Engine service account(s) will not have permission to create and write to the .ldf
file or even read the .mdf
file.
The solution is you have to give file system permissions to the SQL Server database engine service account that runs the SQL instance service. So for me, I gave permissions full control via Windows Explorer to my C:\Temp
path that contained my .mdf
file for the NT Service\MSSQL$SQLEXPRESS01
and NT Service\MSSQL$MSSQL2016
accounts.
Here is a Microsoft article that details this very issue.
To fix this using SQL SERVER Management Studio
Your problem: You get an error such as 'Cannot attach the file 'YourDB.mdf' as database 'YourConnStringNamedContext';
Reason: happens because you deleted the backing files .mdf, ldf without actually deleting the database within the running instance of SqlLocalDb; re-running the code in VS won't help because you cannot re-create a DB with the same name (and that's why renaming works, but leaves the old phantom db name lying around).
The Fix: I am using VS2012, adopt similarly for a different version.
Navigate to below path and enter
c:\program files\microsoft sql server\110\Tools\Binn>sqllocaldb info
Above cmd shows the instance names, including 'v11.0'
If the instance is already running, enter at the prompt
sqllocaldb info v11.0
Note the following info Owner: YourPCName\Username , State: Running , Instance pipe name: np:\.\pipe\LOCALDB#12345678\tsql\query , where 123456789 is some random alphanumeric
If State is not running or stopped, start the instance with
sqllocaldb start v11.0
and extract same info as above.
In the SS Management Studio 'Connect' dialog box enter
server name: np:\.\pipe\LOCALDB#12345678\tsql\query
auth: Windows auth
user name: (same as Owner, it is grayed out for Win. auth.)
Once connected, find the phantom DB which you deleted (e.g. YourDB.mdf should have created a db named YourDB), and really delete it.
Done! Once it's gone, VS EF should have no problem re-creating it.