在实体框架的早期版本中,可以使用 DbMigrator 类以编程方式控制代码优先迁移(例如,检查并应用可用迁移)。该类是否仍然存在于某处或者是否有功能替代?我发现了一篇关于早期 RC 版本的帖子,其中指出了替代品,但 Core 1.0 中似乎也缺少该替代品。我可以通过 CLI 毫无问题地控制迁移,但我认为将需要针对自定义工具场景的代码内解决方案。
功能替换可以在几个地方找到,主要是在 Microsoft.EntityFrameworkCore.Migrations 命名空间中的 API 中。
一些值得一看的地方:
- IHistoryRepository.GetAppliedMigrations() https://github.com/aspnet/EntityFramework/blob/1.0.0/src/Microsoft.EntityFrameworkCore.Relational/Migrations/IHistoryRepository.cs
-
IMigrator.Migrate(string
targetMigration) https://github.com/aspnet/EntityFramework/blob/1.0.0/src/Microsoft.EntityFrameworkCore.Relational/Migrations/IMigrator.cs(这与调用相同DbContext.Database.Migrate() https://github.com/aspnet/EntityFramework/blob/1.0.0/src/Microsoft.EntityFrameworkCore.Relational/RelationalDatabaseFacadeExtensions.cs#L32-L33)
-
IMigrationAssembly https://github.com/aspnet/EntityFramework/blob/1.0.0/src/Microsoft.EntityFrameworkCore.Relational/Migrations/IMigrationsAssembly.cs查找当前迁移
除了IMigrator.Migrate
,使用这些 API 通常意味着将服务从内部 EF Core 的服务容器。 https://docs.efproject.net/en/latest/miscellaneous/internals/services.html这是通过调用来完成的.GetService<TService>()
在你的数据库上下文中。
Example:
var migrator = context.GetService<IMigrator>().Migrate();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)