I would suggest exploring the EF database initializer, in particular the IDatabaseInitializer interface.
If you just want to stop creating the database when it does not exist, just set the Initializer to null . But if you want to register an event or something along these lines, just create your own IDatabaseInitializer - it's not difficult.
You can then set the Application_Start initializer to your global.asax.cs file as follows:
Database.SetInitializer(new YourCustomInitializer());
As a bonus, here is an example of the IDatabaseInitializer that I use to start database migration (using FluentMigrator ) ... it is extremely convenient if I say so myself!
public class MigrationsDbContextInitializer : IDatabaseInitializer<YourDbContext> { private static readonly ILog Logger = LogManager.GetLogger(typeof(MigrationsDbContextInitializer)); public void InitializeDatabase(YourDbContext context) { var announcer = new BaseAnnouncer(x => Logger.Info(x)); var runnerContext = new RunnerContext(announcer) { Database = "sqlserver2008", Connection = context.Database.Connection.ConnectionString, Target = "YourEntitiesNamespace", PreviewOnly = false, Task = "migrate" }; new TaskExecutor(runnerContext).Execute(); } }
source share