Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop EF from trying to create initial DB

So I successfully got my solution to create and initialize the Identity database. The problem now is, as I am working on it I keep getting this error

Cannot create file 'C:\Webs\SomeScheduler\SomeScheduler\App_Data\SomeSchedulerUsers.mdf' 
because it already exists. Change the file path or the file name, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
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 create file 
'C:\Webs\SomeScheduler\SomeScheduler\App_Data\SomeSchedulerUsers.mdf' 
because it already exists. Change the file path or the file name, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

I know that it already exists, that is what I want. How do I stop it from trying to re-create it every time I try to run my code as I develop this site?

As always, any and all help is greatly appreciated.

like image 896
Clint Clark Avatar asked Jul 08 '15 22:07

Clint Clark


2 Answers

Add the following line to your context constructor

Database.SetInitializer<YourContextClass>(null);

This should be enough.

But if you want to create the database only if it doesn't exists and correctly configure migrations, take a look at this question How to avoid recreate an existing database using automatic Code First Migration

like image 88
bateloche Avatar answered Oct 27 '22 08:10

bateloche


You might be able to get away with adding this

<contexts> 
 <context type="Blogging.BlogContext, MyAssembly" disableDatabaseInitialization="true" /> 
</contexts>

to your Web.config. Of course changing Blogging.BlogContext, MyAssembly to more accurately reflect your model.

Source

like image 24
gh0st Avatar answered Oct 27 '22 08:10

gh0st