Entity Framework Core 1.0 code-first migrations using code?

折月煮酒 提交于 2020-01-03 15:20:14

问题


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.


回答1:


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

Some places to look:

  • IHistoryRepository.GetAppliedMigrations()
  • IMigrator.Migrate(string targetMigration) (which is the same as calling DbContext.Database.Migrate())
  • IMigrationAssembly to find current migrations

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.

Example:

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


来源:https://stackoverflow.com/questions/38408213/entity-framework-core-1-0-code-first-migrations-using-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!