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

  • Home
  • SEARCH
  • 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 6845451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:30:08+00:00 2026-05-27T00:30:08+00:00

I have a small MVC 3 app using Entity Framework Code First and use

  • 0

I have a small MVC 3 app using Entity Framework Code First and use this connection string for the model:

data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Journal.mdf;User Instance=true;Database=MyJournal

When I make a change to the model (e.g. add a property), I get as expected

The model backing the ‘JournalContext’ context has changed since the database was created.

So, being in development mode, I go ahead and delete Journal.mdf and Journal.ldf.

Now when I run the application again, I get

Cannot open database “MyJournal” requested by the login. The login
failed.

If I change my connection string to

data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Journal.mdf;User Instance=true;Database=MyJournal2

(changed the Database= parameter by appending ‘2’)

Journal.mdf is created and the app works again. If I make a number of changes and attempt to “recycle” any Database name again, I get the “Cannot open” error.

Why do I need to provide a unique Database name each time I change the model, and how can I “clean out” previous names?

  • 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-05-27T00:30:08+00:00Added an answer on May 27, 2026 at 12:30 am

    You don’t need a unique database name each time. When a model is first created, it runs a DatabaseInitializer to do things like create the database if it’s not there or add seed data. The default DatabaseInitializer tries to compare the database schema needed to use the model with a hash of the schema stored in an EdmMetadata table that is created with a database (when Code First is the one creating the database). If the hash comparison is different then it throws that error.

    Obviously if you change the connection string then its going to create an entirely new database called ‘MyJournal2’.

    Ways to get around this are to delete the EdmMetadata table and run the initializer again. You can do this by going into the Database Explorer window in Visual Studio and connecting to your database, then going to Tables where you should find the EdmMetadata table, right-clicking on it and selecting Delete.

    Alternatively put

    DbDatabase.SetInitializer(new DropCreateDatabaseIfModelChanges<dbType>());
    

    in your Application_Start method in Global.asax.cs. This will delete the database and recreate it whenever the schema changes.

    See this video on pluralsight for more details, especially the section ‘When classes change‘.

    Also check this link for DropCreateDatabaseIfModelChanges. It tells you about what actually happens and how to seed the database if you need to by creating a derived class.

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

Sidebar

Related Questions

I have a small asp.net mvc app running on WIN2k3 and IIS6. I'm using
I have a small app set up using Sring MVC and Sitemesh. When i
I'm thinking of building a small web app using the MVC framework as a
I'm making a small asp.net mvc app. in which I have to compute data
I have just created a mid-sized web-application using Java, a custom MVC framework, javascript.
I have developed a small application using MVC RC2. The application works fine in
I have a small MVC app that works locally, but when I publish it
i have written a small webapp using spring-security and spring-mvc with an annotation based
I'm using EF4.1 in code-first style to persist POCO objects for a small ASP.NET
I have an MVC application that among other things contains a small Silverlight menu

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.