c# - EF 代码第一次迁移 : MigrateDatabaseToLatestVersion without NUGET

标签 c# entity-framework ef-code-first entity-framework-migrations

我需要帮助来阐明 EF Code First Migrations 如何在生产机器上运行。 我有一些实体类和 DbContext 派生类来访问实体。现在,我想执行以下几项操作:

  • 当我的应用程序启动时,它必须创建数据库,如果数据库不存在的话;
  • 然后必须根据模型调整数据库架构;
  • 如果刚才创建了数据库,我想创建一些索引;
  • 另外,如果数据库是刚刚创建的,它必须由一些初始数据作为种子;
  • 所有这些事情都必须自动执行,无需任何 NUGET 命令或外部工具。

我读过一些关于迁移的文章,但它们主要关注运行时的 NUGET 使用或纯自动数据库更新(通过 MigrateDatabaseToLatestVersion)。我知道 DbMigration 类,但我不明白如何将 MigrateDatabaseToLatestVersion 策略和 DbMigration 粘合在一起。

更新。
事实上,我不能在项目中使用 NUGET,我需要“手动”进行迁移的可能性。

最佳答案

只需使用 MigrateDatabaseToLatestVersion

when my application starts, it must create database, if database does not exists;

MigrateDatabaseToLatestVersion 会这样做

then database schema must be adjusted to the model;

MigrateDatabaseToLatestVersion 会这样做

if database was created just now, I want to create some indexes;

为您的数据库和 Up 方法中的 Sql 方法创建基于代码的初始迁移,以定义您需要的所有索引。

also, if database was created just now, it must be seeded by some initial data;

在初始迁移中再次使用 Sql 或在迁移配置中使用 Seed 方法

all of these things must be performed automatically, without any NUGET commands or external tools

NuGet 和命令将帮助您在设计时做好准备(但您可以简单地引用所需的程序集并自己编写所有代码)。运行时不需要任何 powershell 命令。

DbMigrator 适用于您不想使用 MigrateDatabaseToLatestVersion 并且希望从您的代码控制迁移的情况。

关于c# - EF 代码第一次迁移 : MigrateDatabaseToLatestVersion without NUGET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11609080/

相关文章:

c# - 团队环境中 Entity Framework 核心模型快照的问题

c# - 无法将类型 'string' 隐式转换为 'Microsoft.Azure.Management.DataFactory.Models.SecretBase'

entity-framework - Entity Framework 4.3迁移可移动现有数据

c# - 当数据库发生任何变化时,winforms 应用程序中的 Datagridview 会自动更新

c# - MVVM的C#代码优先延迟加载

javascript - 如何在Asp.Net Mvc4中检查验证码后传递Email Id值?

c# - Code First 和 EF 5.0 不加载导航属性

c# - 在 Visual Studio 2010 中调试时出现 fatal error HRESULT=0x80131c08

c# - 获取空错误 LINQ;那么不能使用?

c# - 无法找出具有多个和包含的泛型对象的 C# 泛型