使用代码的Entity Framework Core 1.0代码优先迁移?

编程入门 行业动态 更新时间:2024-10-26 09:21:13
本文介绍了使用代码的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.

一些值得关注的地方:

  • IHistoryRepository.GetAppliedMigrations()
  • IMigrator.Migrate(string targetMigration) (与调用 DbContext.Database.Migrate() )
  • IMigrationAssembly 查找当前迁移
  • IHistoryRepository.GetAppliedMigrations()
  • IMigrator.Migrate(string targetMigration) (which is the same as calling DbContext.Database.Migrate())
  • IMigrationAssembly to find current migrations

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代码优先迁移?

本文发布于:2023-11-15 12:34:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1594489.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   Entity   Framework   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!