实体框架6.1.2中基于代码的数据库迁移:如何避免使用powershell命令?

时间:2022-03-18 19:24:03

I am using EF 6.1.2, to do Code based DB migration there are some commands we need to excecute. Which are: Add-Migration and Update-Database. I want this migration to happen in production environment. So is there any way to avoid Update-Database command in Package Manager Console and use C# APIs to do the same? I can not use Update-Database command in production environment. And I want an aleternate to it in c#.

我使用EF 6.1.2来执行基于代码的数据库迁移,我们需要执行一些命令。其中包括:添加迁移和更新数据库。我希望在生产环境中进行此迁移。那么有没有办法在Package Manager Console中避免Update-Database命令并使用C#API来做同样的事情?我无法在生产环境中使用Update-Database命令。我想在c#中使用它。

1 个解决方案

#1


There is an obvious alternative, you can have the MigrateDatabaseToLatestVersion for this particular db context.

有一个明显的替代方案,您可以为此特定数据库上下文使用MigrateDatabaseToLatestVersion。

This way the migration happens automatically when a very first query is executed against the database. You could even have a separate commandline tool that does the same but is executed at the production environment on demand (rather than when the app executes the very first query), prior to application.

这样,当对数据库执行第一个查询时,迁移会自动发生。您甚至可以使用单独的命令行工具来执行相同操作,但是在应用程序之前,在生产环境中按需执行(而不是在应用程序执行第一个查询时)。

The initializer is straightforward to use, just call:

初始化程序很简单易用,只需调用:

Database.SetInitializer(
           new MigrateDatabaseToLatestVersion<YourContext, Configuration>());

where Configuration is the class that keeps the configuration of your migrations.

其中Configuration是保持迁移配置的类。

#1


There is an obvious alternative, you can have the MigrateDatabaseToLatestVersion for this particular db context.

有一个明显的替代方案,您可以为此特定数据库上下文使用MigrateDatabaseToLatestVersion。

This way the migration happens automatically when a very first query is executed against the database. You could even have a separate commandline tool that does the same but is executed at the production environment on demand (rather than when the app executes the very first query), prior to application.

这样,当对数据库执行第一个查询时,迁移会自动发生。您甚至可以使用单独的命令行工具来执行相同操作,但是在应用程序之前,在生产环境中按需执行(而不是在应用程序执行第一个查询时)。

The initializer is straightforward to use, just call:

初始化程序很简单易用,只需调用:

Database.SetInitializer(
           new MigrateDatabaseToLatestVersion<YourContext, Configuration>());

where Configuration is the class that keeps the configuration of your migrations.

其中Configuration是保持迁移配置的类。