Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Entity Framework migrations in Mono

I've started building an ASP.NET MVC3 project on Mac OS using Xamarin Studio. I now want to add new properties and models to the project but I can't for the life of me work out how to run the Nuget Package Manager console in order to run the Enable-Migrations command.

Am I asking too much? Is this possible or will I have to go back to Visual Studio on Windows?

like image 905
Robin Elvin Avatar asked Dec 04 '13 11:12

Robin Elvin


People also ask

How do I enable-migrations in Package Manager console?

Open the Package Manager Console from Tools → Library Package Manager → Package Manager Console and then run the enable-migrations command (make sure that the default project is the project where your context class is).

How do I use migrations in Entity Framework?

Step 1 − Before running the application you need to enable migration. Step 2 − Open Package Manager Console from Tools → NuGet Package Manger → Package Manger Console. Step 3 − Migration is already enabled, now add migration in your application by executing the following command.

What is automatic migration in Entity Framework?

Automatic Migrations allows you to use Code First Migrations without having a code file in your project for each change you make. Not all changes can be applied automatically - for example column renames require the use of a code-based migration.


1 Answers

All of the Entity Framework Migrations commands are just thin wrappers over an underlying API. To enable migrations, simply create a new class that derives from DbMigrationsConfiguration<TContext> in your project.

For Add-Migration use code similar to the following.

var config = new MyMigrationsConfiguration();
var scaffolder = new MigrationScaffolder(config);
var migration = scaffolder.Scaffold("Migration1");

File.WriteAllText(migration.MigrationId + ".cs", migration.UserCode);

File.WriteAllText(migration.MigrationId + ".Designer.cs", migration.DesignerCode);

using (var writer = new ResXResourceWriter(migration.MigrationId + ".resx"))
{
    foreach (var resource in migration.Resources)
    {
        writer.AddResource(resource.Key, resource.Value);
    }
}

For Update-Database see Running & Scripting Migrations from Code by Rowan Miller.

Update for EF 6.3 👇

A command named ef6.exe has been added to the NuGet package. It contains corresponding commands for each of the PMC commands:

|        PMC        |        ef6.exe        |
| ----------------- | --------------------- |
| Enable-Migrations | ef6 migrations enable |
| Add-Migration     | ef6 migrations add    |
| Update-Database   | ef6 database update   |
| Get-Migrations    | ef6 migrations list   |
like image 145
bricelam Avatar answered Oct 25 '22 04:10

bricelam