添加迁移错误
Errors on Add-Migration
几周前,我在我的项目上启用了迁移,并在 AspNetUsers table.
中添加了一列 'ClientID'
现在我正在尝试添加另一列:"Name"。
我在程序包管理器控制台中 运行 这个命令:
PM> Add-Migration "Name"
我收到以下错误:
Unable to generate an explicit migration because the following
explicit migrations are pending: [20161081751088_InitialCreate,
20161091825212_ClientID]. Apply the pending explicit migrations
before attempting to generate a new explicit migration.
我运行一个更新命令:
PM> Update-Database
但是:
There is already an object named 'AspNetRoles' in the database.
我在网上搜索并找到了这个解决方案:
PM> Add-Migration InitialMigrations -IgnoreChanges
但是我又遇到了第一个错误:
Unable to generate an explicit migration because the following
explicit migrations are pending: [20161081751088_InitialCreate,
20161091825212_ClientID]. Apply the pending explicit migrations
before attempting to generate a new explicit migration.
我该如何解决这个问题?
删除项目中的迁移文件夹
从您的数据库中删除 _MigrationHistory
table。
- 运行 来自包管理器控制台的
Enable-Migrations
命令。
- 运行
Add-Migration Init
创建迁移的命令。
- 删除
Up()
函数中的所有代码行以进行初始迁移。 (创建空迁移并解决您面临的错误。)
- 运行
Update-Database
命令应用你的空迁移。
- 更改代码优先模型以准备添加真正的迁移。
- 运行
Add-Migration InitNew
创建新迁移的命令。
- 运行
Update-Database
命令应用更改。
请先尝试这些:
- 1-清理项目up-root解决方案
- 2-关闭visual studio
- 3-重新打开vs并打开解决方案
- 4- 构建它。
- 5-彻底删除entities文件夹下的migrations文件夹
- 6- 删除 table
来自 DB 的 _projectnameMigrations
- 7-打开VS中的PMC
- 8- Enable-Migrations
- 9- Add-Migrations mig_Myinit
- 10-(!重要)清空up方法,清空
- 11- update-database
- 12- 完成
您的下一次迁移...
重点是,如果您从数据库中手动删除 table,Entity Framework 将无法识别它,默认情况下它是可访问的,因此当您使用 EF 时,您必须执行任何 database-related 任务来自 pmc.
几周前,我在我的项目上启用了迁移,并在 AspNetUsers table.
中添加了一列 'ClientID'现在我正在尝试添加另一列:"Name"。
我在程序包管理器控制台中 运行 这个命令:
PM> Add-Migration "Name"
我收到以下错误:
Unable to generate an explicit migration because the following explicit migrations are pending: [20161081751088_InitialCreate, 20161091825212_ClientID]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
我运行一个更新命令:
PM> Update-Database
但是:
There is already an object named 'AspNetRoles' in the database.
我在网上搜索并找到了这个解决方案:
PM> Add-Migration InitialMigrations -IgnoreChanges
但是我又遇到了第一个错误:
Unable to generate an explicit migration because the following explicit migrations are pending: [20161081751088_InitialCreate, 20161091825212_ClientID]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
我该如何解决这个问题?
删除项目中的迁移文件夹
从您的数据库中删除
_MigrationHistory
table。- 运行 来自包管理器控制台的
Enable-Migrations
命令。 - 运行
Add-Migration Init
创建迁移的命令。 - 删除
Up()
函数中的所有代码行以进行初始迁移。 (创建空迁移并解决您面临的错误。) - 运行
Update-Database
命令应用你的空迁移。 - 更改代码优先模型以准备添加真正的迁移。
- 运行
Add-Migration InitNew
创建新迁移的命令。 - 运行
Update-Database
命令应用更改。
请先尝试这些:
- 1-清理项目up-root解决方案
- 2-关闭visual studio
- 3-重新打开vs并打开解决方案
- 4- 构建它。
- 5-彻底删除entities文件夹下的migrations文件夹
- 6- 删除 table 来自 DB 的 _projectnameMigrations
- 7-打开VS中的PMC
- 8- Enable-Migrations
- 9- Add-Migrations mig_Myinit
- 10-(!重要)清空up方法,清空
- 11- update-database
- 12- 完成
您的下一次迁移... 重点是,如果您从数据库中手动删除 table,Entity Framework 将无法识别它,默认情况下它是可访问的,因此当您使用 EF 时,您必须执行任何 database-related 任务来自 pmc.