DbContextModelSnapshot 何时更新?
When DbContextModelSnapshot is updated?
我是 Entity Framework Core 的新手,大部分时间我都是从 MSDN 网站上自学的。目前我正在学习 Identity
虽然我用的是dotnet core 3.0,但是目前项目使用的是dotnet core 2.2,所以我选择2.2作为Identity学习的起点。
因为我想自定义Identity,所以我使用了以下步骤来启动这个新项目:
新建项目,选择"ASP.NET Core Web Application"、“.NET Core”、"ASP.NET Core 2.2"、"Web Application (Model-View-Controller), change "Authentication”,使用"Individual User Accounts"
使用外部数据库 "ConnectionStrings" 更新 "appsettings.json" 内容
- 在PMC中,使用命令"Update-Database"更新数据库
- 数据库和所有身份 table 已创建,项目 运行 正确
- 通过命令 "Add-migration 1stmigration"
添加迁移以更新 table 和列
- 更新 Up() 和 Down() 函数:
public partial class _1stmigration : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameTable(
name: "AspNetUsers",
newName: "Users");
migrationBuilder.RenameColumn(
name: "Email",
table: "Users",
newName: "EmailAddress");
migrationBuilder.DropIndex(
name: "UserNameIndex",
table: "Users");
migrationBuilder.DropColumn(
name: "NormalizedUserName",
table: "Users");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "NormalizedUserName",
table: "Users");
migrationBuilder.CreateIndex(
name: "UserNameIndex",
table: "Users",
column: "NormalizedUserName");
migrationBuilder.RenameColumn(
name: "EmailAddress",
table: "Users",
newName: "Email");
migrationBuilder.RenameTable(
name: "Users",
newName: "AspNetUsers");
}
}
代码是将默认 "AspNetUsers" table 名称更改为 "Users",并将 "Email" 列更改为 "EmailAddress",并删除 "NormalizedUserName"列和 "UserNameIndex" 索引。
6. 运行 命令 "Update-Database" 更新数据库 table。
数据库 table/column 已根据需要更新,但是当 运行 注册项目以注册新用户时,它 returns 错误:
SqlException: Invalid object name 'AspNetUsers'.
System.Data.SqlClient.SqlCommand+<>c.b__122_0(Task result)
我检查了"Migrations"文件夹中的快照文件,发现创建数据库的代码table没有改变。我的问题是:
1. 当更新快照文件"ApplicationDbContextModelSnapshot.cs"时,我认为应该在Down()和Up()函数有更新代码后执行"Add-migration"命令时更新
2.如何让它运行到前面的步骤完成?
感谢您的帮助。
当您 添加迁移 时 ApplicationDbContextModelSnapshot
在更改 DbContext 后升级,在某种程度上它反映了数据库的实际模式。
您的初始迁移正在初始化身份默认表,我不建议修改它,如果您想更改这些默认表中的某些内容,您可以添加第二个空迁移 Add-Migration
和修改那个,但是 我不推荐 ,相反你可以在你的数据库上下文中改变它,更多在 here.
编辑:更正了我的答案并使其更清晰
我是 Entity Framework Core 的新手,大部分时间我都是从 MSDN 网站上自学的。目前我正在学习 Identity
虽然我用的是dotnet core 3.0,但是目前项目使用的是dotnet core 2.2,所以我选择2.2作为Identity学习的起点。
因为我想自定义Identity,所以我使用了以下步骤来启动这个新项目:
新建项目,选择"ASP.NET Core Web Application"、“.NET Core”、"ASP.NET Core 2.2"、"Web Application (Model-View-Controller), change "Authentication”,使用"Individual User Accounts"
使用外部数据库 "ConnectionStrings" 更新 "appsettings.json" 内容
- 在PMC中,使用命令"Update-Database"更新数据库
- 数据库和所有身份 table 已创建,项目 运行 正确
- 通过命令 "Add-migration 1stmigration" 添加迁移以更新 table 和列
- 更新 Up() 和 Down() 函数:
public partial class _1stmigration : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameTable(
name: "AspNetUsers",
newName: "Users");
migrationBuilder.RenameColumn(
name: "Email",
table: "Users",
newName: "EmailAddress");
migrationBuilder.DropIndex(
name: "UserNameIndex",
table: "Users");
migrationBuilder.DropColumn(
name: "NormalizedUserName",
table: "Users");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "NormalizedUserName",
table: "Users");
migrationBuilder.CreateIndex(
name: "UserNameIndex",
table: "Users",
column: "NormalizedUserName");
migrationBuilder.RenameColumn(
name: "EmailAddress",
table: "Users",
newName: "Email");
migrationBuilder.RenameTable(
name: "Users",
newName: "AspNetUsers");
}
}
代码是将默认 "AspNetUsers" table 名称更改为 "Users",并将 "Email" 列更改为 "EmailAddress",并删除 "NormalizedUserName"列和 "UserNameIndex" 索引。 6. 运行 命令 "Update-Database" 更新数据库 table。 数据库 table/column 已根据需要更新,但是当 运行 注册项目以注册新用户时,它 returns 错误:
SqlException: Invalid object name 'AspNetUsers'.
System.Data.SqlClient.SqlCommand+<>c.b__122_0(Task result)
我检查了"Migrations"文件夹中的快照文件,发现创建数据库的代码table没有改变。我的问题是: 1. 当更新快照文件"ApplicationDbContextModelSnapshot.cs"时,我认为应该在Down()和Up()函数有更新代码后执行"Add-migration"命令时更新 2.如何让它运行到前面的步骤完成?
感谢您的帮助。
当您 添加迁移 时
ApplicationDbContextModelSnapshot
在更改 DbContext 后升级,在某种程度上它反映了数据库的实际模式。您的初始迁移正在初始化身份默认表,我不建议修改它,如果您想更改这些默认表中的某些内容,您可以添加第二个空迁移
Add-Migration
和修改那个,但是 我不推荐 ,相反你可以在你的数据库上下文中改变它,更多在 here.
编辑:更正了我的答案并使其更清晰