EF Core 多個DbContext遷移命令


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

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-database

EntityFrameworkCore\update-database -Context TestDbContext1

其他

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

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 DbContext


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM