我已经搞砸了我的迁移,我在初始迁移上使用了IgnoreChanges,但现在我想删除所有的迁移,并从具有所有逻辑的初始迁移开始。
当我删除文件夹中的迁移并尝试添加- migration时,它不会生成一个完整的文件(它是空的-因为我没有做任何更改,但现在删除了,迁移)。
是否有禁用-迁移命令,以便我可以重新运行启用-迁移?
我已经搞砸了我的迁移,我在初始迁移上使用了IgnoreChanges,但现在我想删除所有的迁移,并从具有所有逻辑的初始迁移开始。
当我删除文件夹中的迁移并尝试添加- migration时,它不会生成一个完整的文件(它是空的-因为我没有做任何更改,但现在删除了,迁移)。
是否有禁用-迁移命令,以便我可以重新运行启用-迁移?
当前回答
问题:你搞砸了你的迁移,你想重置它而不删除现有的表。
问题:您不能重置数据库中现有表的迁移,因为EF希望从头创建表。
应对方法:
Delete existing migrations from Migrations_History table. Delete existing migrations from the Migrations Folder. Run add-migration Reset. This will create a migration in your Migration folder that includes creating the tables (but it will not run it so it will not error out.) You now need to create the initial row in the MigrationHistory table so EF has a snapshot of the current state. EF will do this if you apply a migration. However, you can't apply the migration that you just made as the tables already exist in your database. So go into the Migration and comment out all the code inside the "Up" method. Now run update-database. It will apply the Migration (while not actually changing the database) and create a snapshot row in MigrationHistory.
现在您已经重置了迁移,可以继续进行正常的迁移。
其他回答
这个方法不需要删除__MigrationHistory表,所以你不必在部署时把你的手放在数据库上。
从migrations文件夹中删除现有的迁移。 在包管理控制台运行Add-Migration ResetMigrations 清理Up()方法中的迁移历史:
/// <summary>
/// Reset existing migrations by cleaning the __MigrationHistory table
/// and creating a new initial migration with the current model snapshot.
/// </summary>
public partial class ResetMigrations : DbMigration
{
public override void Up()
{
Sql("DELETE FROM [dbo].[__MigrationHistory]");
}
public override void Down()
{
}
}
VSC(Visual Studio Code) - .Net核心
1.删除状态:删除项目中的迁移文件夹;
2.删除数据库中__MigrationHistory的记录;
3.Dotnet ef数据库drop -v then 您确定要删除服务器上的数据库'<your-database' ?(y / N) 写“N”
4.. dotnet ef迁移添加Initial,然后将20211014110429_initial类的代码写入__MigrationHistory的
Enable-Migrations -EnableAutomaticMigrations -Force
我的问题是手动删除了Migrations文件夹。我这样做是因为我想备份内容,所以我只是将文件夹拖出项目。后来我修复了这个问题,把它放回去(在做了备份副本之后),然后在解决方案资源管理器中右键单击它并从弹出菜单中选择删除,从而删除了Migrations文件夹。
问题:你搞砸了你的迁移,你想重置它而不删除现有的表。
问题:您不能重置数据库中现有表的迁移,因为EF希望从头创建表。
应对方法:
Delete existing migrations from Migrations_History table. Delete existing migrations from the Migrations Folder. Run add-migration Reset. This will create a migration in your Migration folder that includes creating the tables (but it will not run it so it will not error out.) You now need to create the initial row in the MigrationHistory table so EF has a snapshot of the current state. EF will do this if you apply a migration. However, you can't apply the migration that you just made as the tables already exist in your database. So go into the Migration and comment out all the code inside the "Up" method. Now run update-database. It will apply the Migration (while not actually changing the database) and create a snapshot row in MigrationHistory.
现在您已经重置了迁移,可以继续进行正常的迁移。