CodeFirst遷移注意點

2021-07-04 22:40:27 字數 1559 閱讀 8637

context建構函式

不檢查__migrationhistory 取消當資料庫模型發生改變時刪除當前資料庫重建新資料庫的設定。

database.setinitializer(null);

重新建立資料庫

database.setinitializer(new dropcreatedatabaseifmodelchanges()); 

重建資料庫執行,生產環境此法嚴禁使用。

遷移方法1:

automaticmigrationsenabled=true;自動遷移

enable-migrations 啟用遷移

add-migration 為掛起的model變化新增遷移指令碼

update-database 將掛起的遷移更新到資料庫

update-database -verbose 將模型更新到資料庫中並顯示更新指令碼,將更新指令碼複製下來在生產環境中執行

get-migrations 獲取已經應用的遷移

automaticmigrationsenabled=false;非自動遷移

enable-migrations [-contexttypename ] [-enableautomaticmigrations] [-projectname ] [-startupprojectname ] [-connectionstringname ] [-force]

contexttypename:專案繼承自dbcontext的類名字。

enableautomaticmigrations:開啟自動遷移。

projectname:存放dbcontext類的專案名稱。

startupprojectname:解決方案中啟動專案的名稱,作用是呼叫該專案下的連線字串。

connectionstringname:連線字串名稱

上面五個引數是解決問題必須的,其它的無關緊要。

例如:enable-migrations -contexttypename "mvccodefirst.blogentities" -projectname "mvccodefirst" -startupprojectname "mvccodefirst" -connectionstringname "blogentities" -verbose

依次填好之後,問題解決。

同樣的在add-migration、update-database的時候也需要填寫相應的引數。否則會出現同樣錯誤。

例如:add-migration -name "editcst_devicephoto"  -projectname "dbaccesslib" -startupprojectname "website" -connectionstringname "tjssdbcontext" -verbose

update-database -script -projectname "dbaccesslib" -startupprojectname "website" -connectionstringname "tjssdbcontext"  -verbose

遷移方法2:

生產環境中__migrationhistory表刪除,將生產環境中的表與模型更新成一致。

資料遷移注意點

注意點如下 1.程式邏輯部分,新邏輯上線,注意對老邏輯的相容,千萬不要不管三七二十一暴力替換。2.資料庫部分 1 能建新表盡量建新表以避免對老資料的破壞。2 如果老表有欄位增加,千萬不要做非空,唯一性的約束,否則後果自負。3 假如需要減欄位,那麼請考慮臨時替代的方案,比如新建一張臨時表,讓程式先取臨...

MySQL資料庫遷移注意點

廢話先不羅嗦了,就讓這篇草草完成的部落格文章全且當做個記錄吧。以前我使用的是centos上的mysql,後來因為測試需要,需要將 以及在資料庫儲存的資料遷移到新的系統中進行測試,目標系統為redhat enterprise linux server rhel 由於以前從來沒有遷移過資料庫,因此馬上就...

MySQL資料庫遷移注意點

原始出處 作者資訊和本宣告。否則將追究法律責任。廢話先不羅嗦了,就讓這篇草草完成的部落格文章全且當做個記錄吧。以前我使用的是centos上的mysql,後來因為測試需要,需要將 以及在資料庫儲存的資料遷移到新的系統中進行測試,目標系統為redhat enterprise linux server r...