mick_a_p
mick_a_p

Reputation: 101

Add Entity Framework Migration Through Code

I'm wondering if anyone knows how to add a migration through c# code.

I'm kinda new to code first migrations.

I've been playing about with the ToolingFacade class from System.Data.Entity.Migrations.Design.

With this class I can detect migrations, update from one migration to another, detect pending migrations. I can also scaffold a new migration, although I don't know how to "add" this migration to the migrations folder (with the changes in the model handled).

With a newly created scaffold migration, the C# code for this is held within a property. I could in theory create a file in the migrations directory and add this code to it (and then update the database using this migration). Although the pending model changes are not entered within the "up" and "down" methods.

I've also tried using power shell object within C#, using Add-Migration command etc, but they are not recognised as a known cmdlet.

What I want to do is the equivlant of the console command: Add-Migration "MigrationName" -ProjectName "ProjectName" .... in c#

Any help appreciated

Entity framework 4.3 run migrations at application start Does not answer my question. They answer how to update the database with an existing migration. I want to add a new migration, before updating it. By code

Upvotes: 4

Views: 1878

Answers (2)

trailmax
trailmax

Reputation: 35106

I've tried what you are doing and could not do it (though I've managed to get quite a lot of EF migrations stuff through code). As a result I came to abandon this idea because it was a bad idea. Migrations are created at development-time, even pre-compiling. You are trying to execute that during runtime of the application.

Even if you manage to generate a new migration, what are you going to do about the new classes? You need to be able to re-compile your project and run some form of Update-Database on your database with the freshly compiled DLLs.

Also what is the reason for a deployed application to have a new migration? New migrations happen after you change your DB model, i.e. add new classes/properties that are referenced in your DbContext. If your application is compiled and deployed to a client, how can this happen?

From your comments I see that you actually want to roll-back migrations on unsuccessful deployment, rather than create new migrations - that is a whole different story and can be done in C# (let me know if that's what you are looking for, I'll dig out this code for you).

Upvotes: 2

Jack1987
Jack1987

Reputation: 727

Add-Migration [MigrationName]

If you need to specify the domain target:

Add-Migration [MigrationName] -ConfigurationTypeName [ProjectName.Web.Migrations.Configuration]

Well, based on your app and your Entities configuration you can have a method like this:

namespace APP.Migrations
{
    internal class ManualConfiguration : Configuration
    {
        public void ManualSeed(EntitiesDbContext context)
        {
            this.AutomaticMigrationsEnabled = true;
            this.AutomaticMigrationDataLossAllowed = false;

            Database.SetInitializer(new MigrateDatabaseToLatestVersion<Entities.EntitiesDbContext, Configuration>());

            Seed(context);
        }
    }
}

this is a method that you can have, so you can call it from another part of your code

Upvotes: 0

Related Questions