简体   繁体   English

在 Entity Framework Core 中使用 Update-Database 添加新列

[英]Adding new column using Update-Database in Entity Framework Core

May be i'm missing something very obvious.可能是我错过了一些非常明显的东西。 But i haven't been able to figure out a way to add a new column to an existing table/model in EF Core.但是我还没有找到一种方法来将新列添加到 EF Core 中的现有表/模型中。

This is the documentation that I'm following: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell这是我正在关注的文档: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell

And this is what i have done so far:这就是我到目前为止所做的:

  1. Created migration using this command: "Add-Migration -Name CodingSoldierDbContextMigration -OutputDir Migrations -Context CodingSoldierDbContext"使用此命令创建迁移:“Add-Migration -Name CodingSoldierDbContextMigration -OutputDir Migrations -Context CodingSoldierDbContext”
  2. Updated database using the command: "Update-Database -Migration CodingSoldierDbContextMigration -Context CodingSoldierDbContext".使用命令更新数据库:“Update-Database -Migration CodingSoldierDbContextMigration -Context CodingSoldierDbContext”。 Tables got created in the Database.表是在数据库中创建的。
  3. Now i need to add a new column to an existing table.现在我需要在现有表中添加一个新列。 I add that column to the model in the.cs file.我将该列添加到 .cs 文件中的 model 中。 And i remove the existing migration: "Remove-Migration -Force -Context CodingSoldierDbContext"我删除了现有的迁移:“Remove-Migration -Force -Context CodingSoldierDbContext”
  4. Now i re-run the commands in steps 1 and 2. Add-Migration works and migration gets created.现在我重新运行步骤 1 和 2 中的命令。添加迁移工作并创建迁移。 But Update-Database fails with the error: "There is already an object named 'AspNetRoles' in the database."但是更新数据库失败并出现错误:“数据库中已经有一个名为 'AspNetRoles' 的 object。” which means the table is already present in the database which makes sense.这意味着该表已经存在于数据库中,这是有道理的。

So how do i update an already existing table table?那么如何更新已经存在的表表呢? 2 ways i can think of are:我能想到的2种方法是:

  1. Drop the database.删除数据库。 Create migration and update database.创建迁移和更新数据库。 But all data will be gone.但是所有数据都会消失。
  2. Add column in the model.在 model 中添加列。 Manually update the Table using a SQL script to add the new column.使用 SQL 脚本手动更新表以添加新列。

But i feel there should be a better way to do this.但我觉得应该有更好的方法来做到这一点。

This is how i resolved the issue.这就是我解决问题的方法。 Not sure if it is the perfect way.不确定这是否是完美的方式。

Key is NOT to delete the initial migration, before you create the new migration.关键是不要在创建新迁移之前删除初始迁移。

Adding the steps here:在此处添加步骤:

  1. Creating initial migration: "Add-Migration -Name CodingSoldierDbContextMigration -OutputDir Migrations -Context CodingSoldierDbContext"创建初始迁移:“Add-Migration -Name CodingSoldierDbContextMigration -OutputDir Migrations -Context CodingSoldierDbContext”
  2. Updated database using the command: "Update-Database -Migration CodingSoldierDbContextMigration -Context CodingSoldierDbContext".使用以下命令更新数据库:“Update-Database -Migration CodingSoldierDbContextMigration -Context CodingSoldierDbContext”。 Tables gets created in the Database.在数据库中创建表。
  3. Added new field in one of the models.在其中一个模型中添加了新字段。
  4. Creating updated migration: "Add-Migration -Name CodingSoldierDbContextMigrationUpdated -OutputDir Migrations -Context CodingSoldierDbContext".创建更新的迁移:“Add-Migration -Name CodingSoldierDbContextMigrationUpdated -OutputDir Migrations -Context CodingSoldierDbContext”。 This migration will have code only for updating the existing table.此迁移将仅包含用于更新现有表的代码。
  5. Updating DB with the updated migration: "Update-Database -Migration CodingSoldierDbContextMigrationUpdated".使用更新的迁移更新数据库:“Update-Database -Migration CodingSoldierDbContextMigrationUpdated”。 Ideally this should have resolved it.理想情况下,这应该已经解决了。 But for me, it gave error because(as from Verbose logs) it was trying to update with initial migration: "CodingSoldierDbContextMigration", i don't know why.但对我来说,它给出了错误,因为(从详细日志中)它试图通过初始迁移进行更新:“CodingSoldierDbContextMigration”,我不知道为什么。
  6. So i generate scripts using Script-Migration: "Script-Migration -From CodingSoldierDbContextMigration -Idempotent -Output C:\\MigrationScript.sql -Context CodingSoldierDbContext".所以我使用 Script-Migration 生成脚本:“Script-Migration -From CodingSoldierDbContextMigration -Idempotent -Output C:\\MigrationScript.sql -Context CodingSoldierDbContext”。 It generated the script which had changes only from the updated migration.它生成了仅从更新的迁移中进行更改的脚本。 Running that script in DB created the column and added the migration entry in "__EFMigrationsHistory" table.在 DB 中运行该脚本创建了列并在“__EFMigrationsHistory”表中添加了迁移条目。

This is a way which helped me to add a new column to the existed database without losing data:这是一种帮助我在不丢失数据的情况下向现有数据库添加新列的方法:

  1. I've written this command into Package Manager Console to the Project which contains my Model我已将此命令写入包含我的模型的项目的包管理器控制台

    add-migration MyFirstMigration
  2. The above command created a class with a lot of code:上面的命令创建了一个包含大量代码的类:

     public partial class MyFirstMigration : Migration { protected override void Up(MigrationBuilder migrationBuilder) { ... // The code is omitted for the brevity ... } }
  3. I've deleted all generated code and added the following code snippet into the above method Up(MigrationBuilder migrationBuilder) :我删除了所有生成的代码并将以下代码片段添加到上述方法Up(MigrationBuilder migrationBuilder)

     protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn<string>("Password", "Users", "varchar(255)", unicode:false, maxLength: 255, nullable: true); }
  4. Then it is necessary to write the following command into Package Manager Console to add a column into your database:然后有必要将以下命令写入包管理器控制台以将一列添加到您的数据库中:

     Update-Database

I kinda did a mixture of what @Boney and @StepUp suggested and it ran without hitches.我有点混合了@Boney 和@StepUp 的建议,并且运行顺利。 Here's what I did.这就是我所做的。 Assume a have a table Period that I decided to add an additional column PeriodTypeId.假设有一个表 Period,我决定添加一个附加列 PeriodTypeId。

  1. Add another migration.添加另一个迁移。 eg例如

Add-Migration -Name OVCDbMigrationsUpdated -Context DbContext

This created a new migration file with name: OVCDbMigrationsUpdated.这会创建一个名为:OVCDbMigrationsUpdated 的新迁移文件。

  1. Run Update-Database targeting the new migration file.运行针对新迁移文件的 Update-Database。 eg例如

Update-Database -verbose -Migration OVCDbMigrationsUpdated -Context DbContext

My DB was updated successfully with the new column.我的数据库已使用新列成功更新。

I was running into a similar issue with EF Core and an existing database with some of the model fleshed out, but I was adding a new model (that already had a table) and was getting the error我在 EF Core 和一个现有数据库中遇到了类似的问题,其中一些模型已经充实,但是我添加了一个新模型(已经有一个表)并且收到了错误

There is already an object named 'tableN' in the database.

The way I got this to work on an existing database was:我让它在现有数据库上工作的方式是:

  1. Download Repo下载回购
  2. Run all migrations up to date (if you have a database with migrations) If you don't, then just create an initial "dummy" migration and apply that运行所有最新的迁移(如果您有一个带有迁移的数据库)如果没有,那么只需创建一个初始的“虚拟”迁移并应用它
  3. Create a baseline migration prior to making any changes in your model/code在对模型/代码进行任何更改之前创建基线迁移
  4. Update your code with any changes to your database使用对数据库的任何更改更新您的代码
  5. Create another migration创建另一个迁移
  6. Delete the initial baseline migration删除初始基线迁移
  7. update-database更新数据库

Update-Database will work if we exclude previous migration from solution and then run update-database cmd.如果我们从解决方案中排除以前的迁移,然后运行 ​​update-database cmd,则 Update-Database 将起作用。 However we can include again for DB definition.但是,我们可以再次包含数据库定义。 Directly it won't work in case of EF core + postgresql.在 EF 核心 + postgresql 的情况下,它直接不起作用。

One doesn't need to remove the migration or delete the table.不需要删除迁移或删除表。 If you want to add a new column to the database table which was already created using add-migration and update-database, one needs to again run the command (add-migration) in nuget package manager console with a new migration name (add-migration "Name2") and then run the command (update-database).如果要向已使用 add-migration 和 update-database 创建的数据库表添加新列,则需要使用新的迁移名称(add-迁移“Name2”),然后运行命令(更新数据库)。 The system will know what changes have been made and creates a new migration file but the new column will be added to the same database table where you want to add the column.系统将知道已进行了哪些更改并创建一个新的迁移文件,但新列将添加到您要添加该列的同一个数据库表中。

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

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