问题描述
我需要帮助澄清EF的Code First迁移是如何工作的生产机器。
我有一些实体类和的DbContext派生类来访问实体。现在,我想执行这些几件事情:
I need help to clarify how EF Code First Migrations works on production machine.I've some entity classes and DbContext-derived class to access entities. Now, I want to perform these several things:
- 当我的应用程序启动时,它必须建立数据库,如果数据库中不存在的;
- 则数据库模式必须调整模型;
- 如果数据库创建刚才,我想创造一些指标;
- 此外,如果数据库的创建刚才,它必须通过一些初始数据接种
- 所有这些事情必须自动地执行,而没有任何的NuGet命令或外部工具
- when my application starts, it must create database, if database does not exists;
- then database schema must be adjusted to the model;
- if database was created just now, I want to create some indexes;
- also, if database was created just now, it must be seeded by some initial data;
- all of these things must be performed automatically, without any NUGET commands or external tools.
我读过有关迁移的一些文章,但他们主要集中在使用的NuGet或纯自动更新数据库在运行时间(通过 MigrateDatabaseToLatestVersion
)。我知道 DbMigration
类,但我不明白,怎么粘合在一起 MigrateDatabaseToLatestVersion
战略和 DbMigration
。
I've read some articles about migrations, but they're focused mostly on NUGET usage or pure automatic database updates at run-time (via MigrateDatabaseToLatestVersion
). I know about DbMigration
class, but I can't understand, how to glue together MigrateDatabaseToLatestVersion
strategy and DbMigration
.
更新。结果
事实上,我不能使用的NuGet在这个项目,我需要可能,使迁移手动。
UPDATE.
In fact, I cannot use NUGET in the project and I need possibility to make a migration "by hand".
推荐答案
只需使用 MigrateDatabaseToLatestVersion
在我的应用程序启动时,它必须建立数据库,如果数据库中不存在;
MigrateDatabaseToLatestVersion
将这样做。
然后数据库架构必须调整到模型;
MigrateDatabaseToLatestVersion
将这样做。
如果数据库创建刚才,我想创造一些指标;
为你的数据库创建基于代码的初始迁移和的Sql
方法最多
方法来定义你需要的所有索引。
Create code based initial migration for your database and Sql
method in Up
method to define all indexes you need.
另外,如果数据库创建刚才,它必须通过一些种子初始数据;
再次使用的Sql
初始迁移或种子
在迁移配置方法
Again use Sql
in initial migration or Seed
method in migration configuration
所有这些事情必须自动完成,没有任何的NuGet命令或外部工具
的NuGet和命令将帮助你把它在设计时准备(但你可以直接引用所需的程序集,写的所有代码自己)。运行时不需要任何PowerShell命令。
NuGet and commands will help you to prepare it in design time (but you can simply reference required assemblies and write all code yourselves). Runtime doesn't need any powershell commands.
DbMigrator
是情况下,您不想使用 MigrateDatabaseToLatestVersion
,你要控制从您的代码移植。
DbMigrator
is for scenarios where you don't want to use MigrateDatabaseToLatestVersion
and you want to control migration from your code.
这篇关于EF的Code First迁移:MigrateDatabaseToLatestVersion没有的NuGet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!