问题描述
在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.
一些值得关注的地方:
-
(与调用
) - 查找当前迁移
IHistoryRepository.GetAppliedMigrations()
IMigrator.Migrate(stringtargetMigration)
(which is the same as callingDbContext.Database.Migrate()
)IMigrationAssembly
to find current migrations
IMigrator除外。迁移
,使用这些API通常意味着将服务从这是通过在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代码优先迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!