在Mono中启用实体框架迁移 [英] Enable Entity Framework migrations in Mono

查看:164
本文介绍了在Mono中启用实体框架迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Mac OS上使用Xamarin Studio开始构建一个ASP.NET MVC3项目。我现在想为项目添加新的属性和模型,但是在我的生活中,我无法了解如何运行Nuget软件包管理器控制台,以便运行Enable-Migrations命令。


我要求太多了吗?这是可能的还是我必须回到Windows上的Visual Studio?

解决方案

所有的实体框架迁移命令只是在底层API上的薄包装。要启用迁移,只需在项目中创建一个派生自 DbMigrationsConfiguration< TContext> 的新类。



code> Add-Migration 使用类似于以下的代码。

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

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

对于更新数据库请参阅 Running& Rowan Miller编写的代码迁移脚本


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?

解决方案

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.

这篇关于在Mono中启用实体框架迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆