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 8582913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:24:48+00:00 2026-06-11T21:24:48+00:00

I have an ASP.NET MVC 4 project with Entity Framework 5, .NET 4.5 and

  • 0

I have an ASP.NET MVC 4 project with Entity Framework 5, .NET 4.5 and Visual Studio 2012.
In my solution I’ve put all the models in a project called Model, all the Repositories and my DbContext in one more project called Data.
I activate the migrations in the Data project with the Enable-Migrations command. I decide to handle them manually. If I create a new migration with the Add-Migration command everything works very well. If, for example, I add a new column to a table, it works fine. I can see the new column in the database schema and I see the new record into the _MigrationHistory table.
At this point, with the new column created, I need to add this column to the right model. So, i add this method to my code-first model class and I run the project.
It delete my database, and init it with the initial migration.
I can’t tweak a model without loosing all data.
How I can avoid this behavior?
Thanks

UPDATE:

Configuration.cs

namespace NegoziazioneEventi.Data.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<NegoziazioneEventi.Data.NeDataContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(NegoziazioneEventi.Data.NeDataContext context)
        {

        }
    }
}

Application_Start() in Global.asax

protected void Application_Start()
        {

            // init basic data on database
            System.Data.Entity.Database.SetInitializer(new Models.InitData());

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            _container = Bootstrapper.GetWindsorContainer();

        }
  • 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-11T21:24:49+00:00Added an answer on June 11, 2026 at 9:24 pm

    I decide to handle them manually. If I create a new migration with the Add-Migration command everything works very well.
    …
    At this point, with the new column created, I need to add this column to the right model. So, i add this method to my code-first model class and I run the project.

    That is completely wrong usage of migrations and it is also the reason why EF deletes your database. You must first add property to model and then add migration because EF needs to store correct data into _MigrationHistory table to match that record with the real meaning of that migration.

    With your current approach EF runs the application and checks _MigrationHistory table but the record in the table doesn’t contain information about your newly added property so EF believes that new change was done to your model and uses default strategy to delete database and create a new one reflecting your current model. You can turn off this behavior but you should start by using migrations correctly!

    To turn off the behavior use:

    Database.SetInitializer<YourDatabaseContext>(null);
    

    You are using your own initializer which is most probably derived from wrong build-in initializer causing drop of your current database.

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

Sidebar

Related Questions

In Visual Studio 2010 I have an ASP.NET MVC 3 project called blahblah. As
I have a ASP.NET MVC 4 project - CodeFirst with Entity Framework - that
I have a project with ASP.NET MVC, using Entity Framework to get data from
I am developing a medium sized ASP.NET project using ASP.NET MVC and Entity Framework.
I have an ASP.NET MVC 3 (using Entity Framework 4.2) application that uses transactions
I use EntityFramework on my ASP.NET MVC project. Let's say I have the entity
In my ASP.NET MVC application, I have a project that contains all the business
I currently have a POCO class in my ASP.NET MVC project that handles all
I have ASP.Net MVC project. Plus wordpress blog under a subfolder /Blog. Now, when
I have an ASP.NET MVC project with xVal and data annotations and I need

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.