Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8642697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:50:36+00:00 2026-06-12T11:50:36+00:00

Sometimes, I’ll delete my development database and run my EF code-first application. I’ll get

  • 0

Sometimes, I’ll delete my development database and run my EF code-first application. I’ll get the error:

Cannot open database “AssessmentSystem” requested by the login. The login failed.
Login failed for user ‘AssessmentAdmin’.

I think this is because the DbContext only runs the DB initialization logic “once per AppDomain when the context is used for the first time”, as this page says. This means I need to recycle the IIS application pool to get EF to recreate my database if I drop it.

Is there any way I can get the DB initialization code to run every time I try to access the database? So, it will always check to see whether the DB exists and if not, create it instead of trying to open it, even if it’s using the same AppDomain that previously accessed the database that I have now dropped?

Note that I would like this initializtion check to be done on every query, so even having it done in Application_Start isn’t often enough; ideally, I’d like to be able to load some DB data, delete the DB, then load some DB data and it would recreate the DB without my even having to restart the application (basically I would just have to reload the web page that loads some DB data).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:50:37+00:00Added an answer on June 12, 2026 at 11:50 am

    Initializer is executed when you need to access the database so if you want to create database on app start use anything of the following:

        context.Database.Initialize(true); //If set to true the initializer is run even if it has already been run.       
        context.Database.Create()
    

    http://msdn.microsoft.com/en-us/library/system.data.entity.database.initialize(v=vs.103).aspx

    CreateDatabaseIfNotExists
    An implementation of IDatabaseInitializer that will recreate and optionally re-seed the database with data only if the database does not exist. To seed the database, create a derived class and override the Seed method.

    Database.SetInitializer<MyContext>(new CreateDatabaseIfNotExists<MyContext>());
    

    http://msdn.microsoft.com/en-us/library/gg679221(v=vs.103).aspx

    DropCreateDatabaseIfModelChanges
    An implementation of IDatabaseInitializerthat will delete, recreate, and optionally re-seed the database with data only if the model has changed since the database was created. This is achieved by writing a hash of the store model to the database when it is created and then comparing that hash with one generated from the current model. To seed the database, create a derived class and override the Seed method.

    The initialization strategy can optionally check for database
    existence, create a new database, and seed the database with data. The
    default strategy is an instance of
    CreateDatabaseIfNotExists.

    Database.SetInitializer(new DropCreateDatabaseIfModelChanges());

    Note that this assumes you have permission to even drop your database.

    http://msdn.microsoft.com/en-us/library/gg679604(v=vs.103).aspx

    DropCreateDatabaseAlways

    An implementation of IDatabaseInitializer that will always recreate and optionally re-seed the database with data the first time that a context is used in the application domain. To seed the database, create a derived class and override the Seed method.

    Database.SetInitializer<MyContext>(new DropCreateDatabaseAlways<MyContext>());
    

    http://msdn.microsoft.com/en-us/library/gg679506(v=vs.103).aspx

    I recommend that you look at Migrations if you want to track, revert the changes you made to your DB to the previous state http://msdn.microsoft.com/hr-hr/data/jj591621 .

    UPDATE

    context.Database.Initialize(true);
    

    If the parameter force is set to true, then the initializer is run
    regardless of whether or not it has been run before. This can be
    useful if a database is deleted while an app is running and needs to
    be reinitialized.

    For MVC application add a section to the Application_Start() method in the Global.asax

    protected void Application_Start() {
    
         Database.SetInitializer<MyContext>(new DropCreateDatabaseAlways<MyContext>()); 
    
         // Forces initialization of database on model changes.
         using (var context= new MyContext()) {
              context.Database.Initialize(force: true);
         }    
    }
    

    Also you can use a custom initializer :

    public class MyDbInit : DropCreateDatabaseAlways<MyContext>
    {
    
    }
    

    and then use

    Database.SetInitializer(new MyDbInit());
    

    UPDATE 2

    Make a new empty MVC4 application called DeleteDBOnEveryRequest.
    Put the following in the Global.asax Application_start

    protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
    
                WebApiConfig.Register(GlobalConfiguration.Configuration);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
    
                Database.SetInitializer<BlogContext>(new DropCreateDatabaseAlways<BlogContext>());    
    
                using (var context = new BlogContext())
                {
                    context.Database.Initialize(force: true);
                }    
            }
    

    Make a new controller called DatabaseController with two actions.

    In the Access action you delete the DB and redirect to Recreated action from where you create a DB as it was previousy deleted.

    namespace DeleteDBOnEveryRequest.Controllers
    {
        public class DatabaseController : Controller
        {
            public ActionResult Access()
            {
                using (var context = new BlogContext())
                {
                    context.Database.Delete();
                } 
                return RedirectToAction("Recreated");
            }
    
            public ActionResult Recreated()
            {
                using (var context = new BlogContext())
                {
                    context.Database.Initialize(force: true);                
                }
                return View();
            }
        }
    }
    

    Is this what you wanted?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes when I'm trying to run my code, the Eclipse console says: Error in
Sometimes it's very difficult to find an error in an application because of static
Sometimes a query works sometimes it doesn't. I get sometimes Virtuoso S1T00 Error SR171:
Sometimes, tomcat restart will cause my application stop immediately so that my consuming data
Sometimes I get errors that I suspect are the result of my Django app
Sometimes when debugging with my application I encounter Contract.Requires() failing due to the condition
Sometimes I see code like this: LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL&
Sometimes I'd like to print my code and read it during lunch. In Eclipse
Sometimes I want to run a maximum amount of IO actions in parallel at
Sometimes my application is crashing on CFRelease(theURL); CFURLRef theURL = CFURLCreateFromFSRef( kCFAllocatorDefault, inRef );

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.