1

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();

        }
2
  • Can you post your Configuration.cs code from Migrations folder and Application_Start method from the Global.asax? Commented Sep 26, 2012 at 9:50
  • I've post them in the question. Thanks Commented Sep 26, 2012 at 10:02

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. I can't find a complete reference documentation about Entity Framework so I'm trying without knowledge.
If anyone needs a tutorial to how make a migration: asp.net/web-forms/tutorials/deployment/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.