在Entity Framework Core中使用Update-Database添加新列 [英] Adding new column using Update-Database in Entity Framework Core
问题描述
可能是我遗漏了一些非常明显的东西。但是我还没有办法在EF 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.
这是我正在使用的文档以下: https://docs.microsoft.com/en-us / ef / core / miscellaneous / cli / powershell
This is the documentation that I'm following: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell
这是我到目前为止所做的:
And this is what i have done so far:
- 使用以下命令创建迁移:添加-迁移-名称CodingSoldierDbContextMigration -OutputDir迁移-Context CodingSoldierDbContext
- 使用以下命令更新数据库: -数据库-迁移CodingSoldierDbContext迁移-上下文CodingSoldierDbContext。表已在数据库中创建。
- 现在,我需要向现有表中添加新列。我将该列添加到.cs文件中的模型中。然后,我删除现有的迁移: Remove-Migration -Force -Context CodingSoldierDbContext
- 现在,我在步骤1和2中重新运行命令。Add-Migration工作并创建了迁移。但是Update-Database失败并显示以下错误:数据库中已经有一个名为‘AspNetRoles’的对象。这意味着该表已经存在于数据库中,这很有意义。
- Created migration using this command: "Add-Migration -Name CodingSoldierDbContextMigration -OutputDir Migrations -Context CodingSoldierDbContext"
- Updated database using the command: "Update-Database -Migration CodingSoldierDbContextMigration -Context CodingSoldierDbContext". Tables got created in the Database.
- Now i need to add a new column to an existing table. I add that column to the model in the .cs file. And i remove the existing migration: "Remove-Migration -Force -Context CodingSoldierDbContext"
- Now i re-run the commands in steps 1 and 2. Add-Migration works and migration gets created. But Update-Database fails with the error: "There is already an object named 'AspNetRoles' in the database." which means the table is already present in the database which makes sense.
那我该如何更新已经存在的表table?
我能想到的2种方法是:
So how do i update an already existing table table ? 2 ways i can think of are:
- 删除数据库。创建迁移并更新数据库。但是所有数据都将消失。
- 在模型中添加列。使用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.
在此处添加步骤:
- 创建初始迁移:添加迁移-名称CodingSoldierDbContextMigration -OutputDir迁移-Context CodingSoldierDbContext
- 使用以下命令更新数据库:更新数据库-Migration CodingSoldierDbContextMigration -Context CodingSoldierDbContext。在数据库中创建表。
- 在其中一个模型中添加了新字段。
- 创建更新的迁移:添加迁移-名称CodingSoldierDbContextMigrationUpdated- OutputDir迁移-Context CodingSoldierDbContext。此迁移将仅具有用于更新现有表的代码。
- 使用已更新的迁移来更新数据库: Update-Database -Migration CodingSoldierDbContextMigrationUpdated。理想情况下,这应该已经解决了。但是对我来说,它给出了错误,因为(例如从Verbose日志中)它正尝试通过初始迁移进行更新: CodingSoldierDbContextMigration,我不知道为什么。
- 因此,我使用以下命令生成脚本脚本迁移:脚本迁移-从CodingSoldierDbContextMigration-幂等-输出C:\MigrationScript.sql -Context CodingSoldierDbContext。它生成的脚本仅在更新后的迁移中有所更改。在数据库中运行该脚本会创建该列,并将迁移条目添加到 __EFMigrationsHistory表中。
- Creating initial migration: "Add-Migration -Name CodingSoldierDbContextMigration -OutputDir Migrations -Context CodingSoldierDbContext"
- Updated database using the command: "Update-Database -Migration CodingSoldierDbContextMigration -Context CodingSoldierDbContext". Tables gets created in the Database.
- Added new field in one of the models.
- Creating updated migration: "Add-Migration -Name CodingSoldierDbContextMigrationUpdated -OutputDir Migrations -Context CodingSoldierDbContext". This migration will have code only for updating the existing table.
- Updating DB with the updated migration: "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.
- So i generate scripts using 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.
这篇关于在Entity Framework Core中使用Update-Database添加新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!