Darion Badlydone
Darion Badlydone

Reputation: 947

Entity Framework 5 delete my database changing model

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

        }

Upvotes: 1

Views: 2146

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364279

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.

Upvotes: 2

Related Questions