当前位置:Gxlcms >
数据库问题 >
EntityFramework Core Code First 已有数据库
EntityFramework Core Code First 已有数据库
时间:2021-07-01 10:21:17
帮助过:18人阅读
partial class init : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "AspNetRoles",
columns: table =>
new
{
Id = table.Column<
string>(nullable:
false),
Name = table.Column<
string>(maxLength:
256, nullable:
true),
NormalizedName = table.Column<
string>(maxLength:
256, nullable:
true),
ConcurrencyStamp = table.Column<
string>(nullable:
true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetRoles", x =>
x.Id);
});
....
}
如果直接执行 update-database 命令的话会发现执行会报错。那怎么办呢,是不是已有数据库的情况下再也不能用Code First了?
当然不是啦,经过测试,只需要按一下方法,就能有效的把实体的更改更新到数据库了。
首选,需要确保目前的实体和数据库表一致,接着执行迁移命令 Add-Migration init。
接着,在Migrations目录中找到生成的 xxx_init.cs 文件,进去把 Up() Down()方法中的代码都去掉,只留下空方法体。
接着,执行 Update-Database命令。这样就完成了迁移的初始化。完成这一步后,以后再修改实体就能顺利的使用迁移命令了。
EntityFramework Core Code First 已有数据库
标签:over prot ini zed bsp 行迁移 迁移 怎么 art