12

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?

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
Robin Elvin
  • 1,207
  • 12
  • 28

1 Answers1

17

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   |
bricelam
  • 28,825
  • 9
  • 92
  • 117
  • 3
    Seems like a pain in the buttocks – BRogers Mar 09 '14 at 06:42
  • Still cool that you posted a solution though... not downplaying you at all... just wish it had it built in – BRogers Mar 09 '14 at 06:43
  • @bricelam, where can you run this code? Should I make a separate console project? Or call a controller action? – user2609980 Nov 21 '14 at 17:21
  • 1
    @user2609980 I'd recommend a separate console app. – bricelam Nov 21 '14 at 19:18
  • @bricelam I have used a separate console app to include the code and run the migration, but the project with the Models and database context is referenced. – user2609980 Nov 21 '14 at 19:19
  • To workaround the MARS limitation, you'll have to use more eager loading. – bricelam Nov 21 '14 at 19:24
  • Hi @bricelam first of all thanks for responding, secondly a question regarding the eager loading: how can I implement that? The exception "MARS is not implemented" is thrown because of some setting somewhere where I have no idea where it is set. I posted a question here: http://stackoverflow.com/questions/27069403/how-to-circumvent-mars-is-not-yet-implemented-exception. – user2609980 Nov 21 '14 at 22:20
  • Let me know if you have an idea or not, if not I will not try any more and just write the database scripts that map to the models myself. The code can be found [here](https://github.com/erooijak/zaaikalender) in the Zk.Migrations project, and I run on latest version of Mono. Thanks. – user2609980 Nov 22 '14 at 07:41
  • @bricelam Could you shine your light on [this question](http://stackoverflow.com/questions/27115642/what-files-am-i-missing-wile-running-migrations-from-a-console-app). I cannot get the `Update-Database` to work because a resource file is missing. – user2609980 Nov 25 '14 at 01:00
  • based on the [open source repo](https://github.com/aspnet/EntityFramework6) - it's a pretty big stretch to call them "thin wrappers". There's a lot going on in the power shell scripts. – aaaaaa Jul 06 '17 at 02:52