Search code examples
c#asp.netentity-frameworkasp.net-identity

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.


Solution

  • 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