EF Core 多個DbContext遷移命令

若是涉及多個項目,注意保持DbContext所在項目和啓動項目關於數據庫的包引用版本一致
注意設置不一樣的DbContext遷移文件目錄不一樣
1.Enable-migrationsshell

EntityFrameworkCore\Enable-migrations –ContextTypeName:Namespace.TestDbContext1 –MigrationDirectory:MigrationDirectory1 
EntityFrameworkCore\Enable-migrations –ContextTypeName:Namespace.TestDbContext2 –MigrationDirectory:MigrationDirectory2

2.Add-Migration數據庫

EntityFrameworkCore\Add-migration migration_name -Context TestDbContext1 -OutputDir MigrationDirectory1

必須指明-Context參數,參數爲DbContext類名
3.update-databasespa

EntityFrameworkCore\update-database -Context TestDbContext1

其餘

存在多個DbContext的時候不指名Context的報錯內容code

More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.

參考資料

Entity Framework Core tools reference - Package Manager Console in Visual Studio
Entity Framework - Multiple DbContext
Code First Migration in Multiple DbContexthtm

相關文章
相關標籤/搜索