4

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

4
  • Have you tried running enable-migrations from the NuGet Package Manager Console ? Commented Sep 18, 2015 at 12:21
  • Migrations are enabled - I am able to do power shell commands through the package manager console (Add-Migration, Update-Datbase etc), but this is not what I'm after. I'm looking handle these commands through code. Commented Sep 18, 2015 at 12:24
  • possible duplicate of Entity framework 4.3 run migrations at application start Commented Sep 18, 2015 at 12:28
  • 1
    No .. its not. I have seen this question.. They answer how to update the database with an existing migration. I want to add a new migration, before updating it. By code Commented Sep 18, 2015 at 12:30

2 Answers 2

2

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).

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

Comments

0
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

3 Comments

Thanks for your reply I'm not looking to do this through power shell commands.. I know how to do migrations through these commands, and it works when I do it (through Package Manager Console). I'm looking to execute power shell commands from c#.. or find another equivalent method from EF classes that allows me to add-migrations The ToolingFacadde class within EF allows me to manage migrations, but I cannot seem to find a way to add a new one.
it is viable for you to do it through an url? You have a web app?
No sorry, its a desktop app.. We could just leave the the database initializer as automatic migrations (MigrateDatabaseToLatestVersion), but I want more control over this. I want to be able to revert back to a different migration, if say for example the new one fails. I need this to be done by code, as we wont have a database administrator to handle the database changes, entering power-shell commands or executing a generated script.

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.