第一次使用時需要在“程序包管理控制臺”輸入:Enable-Migrations 如果已經創建過的則直接跳過。
項目根目錄會自動創建一個文件夾 Migrations。
第二步輸入:Add-Migration AddUserTable
第三步輸入:Update-Databse
就能新建一張User表,前提是User的Map、Model先創建好。
詳細步驟可以查看此處:http://www.cnblogs.com/panchunting/p/entity-framework-code-first-migrations.html
問題:No connection string named 'ManageMigDbContext' could be found in the application config file。
你的默認項目是否選擇正確,根目錄是否創建'ManageMigDbContext' 文件了,如果沒有創建,則不行。
如果項目創建DbContext文件了,只是目錄不是在根項目里,比如在別的文件夾里,你使用Add-Migration AddUserTable 是不行的,需要指定DbContext的空間命名
比如:Add-Migration AddUserTable -StartUpPRojectName DbMigration.Manage.ManageMigDbContext ManageMigDbContext 的文件在Manage文件夾里
同時后面的 Update-Databse -StartUpProjectName DbMigration.Manage.ManageMigDbContext 也需要指定。
再運行后出現紅色提示Get-Project : 找不到項目“DbMigration.Manage.ManageMigDbContext” 不用管,直接忽視,查看數據就知道是否成功。
Migrations的幫助命令:get-help Add-Migration -full
若要查看示例,請鍵入: "get-help Add-Migration -examples".有關詳細信息,請鍵入: "get-help Add-Migration -detailed".若要獲取技術信息,請鍵入: "get-help Add-Migration -full".
其他的命名都是一樣的模式,只是把get-help Add-Migration -full 中間的Add-Migration替換即可
新聞熱點
疑難解答