[英]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:这就是我到目前为止所做的:
So how do i update an already existing table table?那么如何更新已经存在的表表呢? 2 ways i can think of are:
我能想到的2种方法是:
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:在此处添加步骤:
This is a way which helped me to add a new column to the existed database without losing data:这是一种帮助我在不丢失数据的情况下向现有数据库添加新列的方法:
I've written this command into Package Manager Console to the Project which contains my Model我已将此命令写入包含我的模型的项目的包管理器控制台
add-migration MyFirstMigration
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 ... } }
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); }
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。
Add-Migration -Name OVCDbMigrationsUpdated -Context DbContext
This created a new migration file with name: OVCDbMigrationsUpdated.这会创建一个名为:OVCDbMigrationsUpdated 的新迁移文件。
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:我让它在现有数据库上工作的方式是:
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.