使用代码的Entity Framework Core 1.0代码优先迁移? [英] Entity Framework Core 1.0 code-first migrations using code?

查看:113
本文介绍了使用代码的Entity Framework Core 1.0代码优先迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Entity Framework的先前版本中,可以通过DbMigrator类以编程方式控制代码优先的迁移(例如,检查并应用可用的迁移)。该类还存在还是某个功能替代品?我在早期的RC版本中发现了一个帖子,指出了替代版本,但Core 1.0似乎也没有。我可以通过CLI毫无问题地控制迁移,但是我认为将需要针对自定义工具方案的代码内解决方案。

In previous versions of Entity Framework code-first migrations could be controlled programmatically with the DbMigrator class (e.g. check for and apply available migrations). Does that class still exist somewhere or is there a functional replacement? I found a post on an early RC version that indicated a substitute but that too seems to be missing from Core 1.0. I can control my migrations through CLI without issue but I think an in-code solution for custom tooling scenarios is going to be needed.

推荐答案

可以在一些地方找到功能替代品,主要是在Microsoft.EntityFrameworkCore.Migrations命名空间的API中找到。

The functional replacement can be found in a few places, primarily in the API found in the Microsoft.EntityFrameworkCore.Migrations namespace.

一些值得关注的地方:

IMigrator除外。迁移,使用这些API通常意味着将服务从内部EF Core的服务容器。这是通过在dbcontext上调用 .GetService< TService>()来完成的。

With the exception of IMigrator.Migrate, using these API usually means pulling the service out of internal EF Core's service container. This is done by calling .GetService<TService>() on your dbcontext.

示例:

var migrator = context.GetService<IMigrator>().Migrate();

这篇关于使用代码的Entity Framework Core 1.0代码优先迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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