简体   繁体   English

实体框架数据库优先 - 如何防止运行初始迁移?

[英]Entity Framework Database First - How to prevent running the initial migration?

I was able to scaffold an existing database using EF Core recently.我最近能够使用 EF Core 搭建一个现有的数据库。 We want to completely migrate to EF Core, but we've hit some snags and I need your help.我们希望完全迁移到 EF Core,但我们遇到了一些障碍,我需要您的帮助。

We began by running the scaffold command and ended up with this file structure我们从运行 scaffold 命令开始,最后得到这个文件结构

C:\>ls .\EFCore
Context
Models
EFCore.csproj

Then after creatinng the Context and the Models, I ran the inital migration command.然后在创建上下文和模型之后,我运行了初始迁移命令。

dotnet ef migrations add CreateDb

After running that command our directories look like this运行该命令后,我们的目录如下所示

C:\>ls .\EFCore
Context
Models
Migrations
EFCore.csproj

C:\>ls .\EFCore\Migrations
20210616210256_CreateDb.cs
20210616210256_CreateDb.Designer.cs
DbContextModelSnapshot.cs

My Problem我的问题

The database is already created in all environments, so we don't want entity to execute the inital database migration.数据库已经在所有环境中创建,所以我们不希望实体执行初始数据库迁移。 Should I delete 20210616210256_CreateDb.cs and 20210616210256_CreateDb.Designer.cs to achieve this?我应该删除20210616210256_CreateDb.cs20210616210256_CreateDb.Designer.cs来实现这一点吗?

The other route I can think of is create the __EFMigrationsHistory table and insert a row into it.我能想到的另一条路线是创建__EFMigrationsHistory表并向其中插入一行。 This would fake that we've already ran the inital db create.这会假装我们已经运行了初始数据库创建。

Help please, I would like to do this the cleanest way possible.请帮助,我想以最干净的方式做到这一点。

Remove/Comment the code inside your Up() and Down() methods of migration file and call update database.删除/注释迁移文件的 Up() 和 Down() 方法中的代码并调用更新数据库。 You don't want the code which creates table ie你不想要创建表格的代码,即

CreateTable(
            "dbo.YourTable",
            c => new
                { //Properties set here })

Your migration class could look like this -您的迁移课程可能如下所示 -

public partial class Initial : DbMigration
{
    public override void Up()
    {   
    }
    
    public override void Down()
    {
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM