我已经搞砸了我的迁移,我在初始迁移上使用了IgnoreChanges,但现在我想删除所有的迁移,并从具有所有逻辑的初始迁移开始。
当我删除文件夹中的迁移并尝试添加- migration时,它不会生成一个完整的文件(它是空的-因为我没有做任何更改,但现在删除了,迁移)。
是否有禁用-迁移命令,以便我可以重新运行启用-迁移?
我已经搞砸了我的迁移,我在初始迁移上使用了IgnoreChanges,但现在我想删除所有的迁移,并从具有所有逻辑的初始迁移开始。
当我删除文件夹中的迁移并尝试添加- migration时,它不会生成一个完整的文件(它是空的-因为我没有做任何更改,但现在删除了,迁移)。
是否有禁用-迁移命令,以便我可以重新运行启用-迁移?
当前回答
如何
Update-Database –TargetMigration: $InitialDatabase
在包管理器控制台?它应该将所有更新重置到非常早期的状态。
参考链接:代码优先迁移-迁移到特定版本(包括降级)
其他回答
UPDATE 2020 =>重置实体-框架迁移
Add-Migration Initial -Context ApplicationDbContext
ApplicationDbContext =>您的上下文。
但是如果你只需要更新一个存在的标识模式,试试它: https://stackoverflow.com/a/59966100/4654957
删除迁移文件夹,清理然后重建项目。这对我很管用。在清理和重建之前,它说迁移已经存在,因为在它的缓存内存中,它还没有删除。
问题:你搞砸了你的迁移,你想重置它而不删除现有的表。
问题:您不能重置数据库中现有表的迁移,因为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.
现在您已经重置了迁移,可以继续进行正常的迁移。
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的
对于 EFCore 6/7:
为了将所有更改合并到单个文件中,并将其应用到现有数据库中,同时保留所有数据(即在Production和其他开发中),以下步骤对我来说很有效:
Delete existing migrations from the Migrations Folder in your project. Delete the Context Snapshop files. I.e. ApplicationDbContextSnapshot.cs, DatabaseContextModelSnapshot.cs or similar. Rebuild your solution Run Add-Migration Initial. (Can be named how you like instead of Initial). This will create a migration in your Migration folder that includes creating the tables, but won't be applied yet. Open the generated Migration file and comment out all the code inside the "Up" method, so that it doesn't make any changes to all your existing tables. (This allows us to update the database in the correct format, without making any changes in the next step) Before the commented code within the Up function, add migrationBuilder.Sql("DELETE FROM [dbo].[__EFMigrationsHistory]"); to have the delete/truncate applied during migration
public partial class Initial : Migration
{
public override void Up()
{
migrationBuilder.Sql("DELETE FROM [dbo].[__EFMigrationsHistory]");
/*
OTHER COMMENTED OUT CODE
*/
}
public override void Down()
{
}
}
Run Update-Database. It will apply the Migration and remove existing migration tracking entriers (while not actually changing the database schema) and create a snapshot row in __EFMigrationsHistory Submit this code and apply to your dev, staging and production databases as you normally would (Ensure the code is still commented out in the Up function, no changes will be applied, except a new entry created in __EFMigrationsHistory) Remove migrationBuilder.Sql("DELETE FROM [dbo].[__EFMigrationsHistory]"); and uncomment all the code from step 5 and submit back to source control. (This way new devs can create the db from scratch. If someone forgot to update their existing database, it'll error out and complain that the tables already exist, so they can just comment the Initial file out and apply.)