iT邦幫忙

3

ASP.NET MVC 從 Database First 回到 EF Migrations 管理 Model 與資料庫(MS-SQL)

前言

最近剛好有個專案,前台及資料庫的環境都已經建置完成了,不過現在還需要個管理的後台,於是想說使用 MVC 架構來開發。
但因為以往 MVC 開發新專案時,基本上都還沒有建立資料庫的環境,所以都是由 CodeFirst 撰寫程式類別,建立 Entity Data Model 後,並透過 Migrations 將 Model 的異動更新至資料庫。

但這次資料庫環境都已經建置好的情況下,所以只能由現有的資料庫來建立 Data Model 了,那就是 DataBase First。

不過建立好的 Data Model 若有修改並要更新至資料庫,必須回到 Code First 才可 EF Migrations 更新資料庫,所以來紀錄一下,先 Database First 後 Code First 的作法。

Database First 建立 Data Model

對 Model 資料夾 -> 加入 -> 新增項目 -> 資料 -> ADO.NET 實體資料模型 -> 取好名字( DbContext 的名字 )後下一步

https://ithelp.ithome.com.tw/upload/images/20200215/20119925kC8mi24B1s.png

選擇來自資料庫的 Code First 然後下一步

https://ithelp.ithome.com.tw/upload/images/20200215/20119925IITCSXaVAA.png

接下來要來做與資料庫的連線設定,按下新增連線設定伺服器名稱與驗證方式還有要跟哪個資料庫做連線。

https://ithelp.ithome.com.tw/upload/images/20200215/20119925JHwnyJTXCM.png

https://ithelp.ithome.com.tw/upload/images/20200215/20119925yFmsOy8b1Q.png

確定後,會自動產生連接字串,並且將 Web.Config 的連線設定儲存為的 Checkbox 打勾,精靈會在 Web.Config 自動產生連接字串。

https://ithelp.ithome.com.tw/upload/images/20200215/201199256YRN2FDYs5.png

下一步,選擇要建立的資料表或 View ,選好後按確定就會自動產生 Data Model 了

https://ithelp.ithome.com.tw/upload/images/20200215/20119925COUVFycEY5.png

https://ithelp.ithome.com.tw/upload/images/20200215/20119925zbQOac0g4S.png

也自動建立好的 DbContext 和 Data Model 不過標籤還需是需要自己寫的XDD

https://ithelp.ithome.com.tw/upload/images/20200215/20119925ZQ2lFjfokc.png

https://ithelp.ithome.com.tw/upload/images/20200215/20119925sf7LZtmFzb.png

現在 Model 環境建立好了,但假如之後有修改 Model 類別內的 Framework 程式碼要如何將 Model 的異動更新至資料庫呢?

回到Code First 透過 Migrations 管理 Model 與資料庫

使用 Code First 建置資料環境時,需要先建立一個 Mmigrations 資料夾。

現在在 Database First 建立好 Data Model 時要更新資料庫也一樣。

先在套件管理主控台輸入

Enable-Migrations

建立好 Mmigrations 資料夾後,接下來在更新 Model 內的程式後,跟以往一樣,在套件管理主控台輸入

Add-Migration [自己想取的名字]

類似更新資料庫的紀錄,輸入後會發現,UP()內的程式碼會將全部的 Model 都準備重新 Create。

https://ithelp.ithome.com.tw/upload/images/20200215/201199252lTpW7WA0v.png

若這時候像以往一樣再輸入

update-database

那鐵定是會發生錯誤,因為資料庫內原本就有這些資料表(Table)了。

https://ithelp.ithome.com.tw/upload/images/20200215/20119925IG846qmjTR.png

所以需要在資料庫內手動輸入建立 MigrationHistory Table 的 SQL語法,所以在套件管理主控台輸入

update-database -Script

會取得一個SQL檔,會有新增 Model Table 的SQL語法以及新增 MigrationHistory Table 的SQL語法,那我們只需要新增 MigrationHistory 的SQL語法就好,所以只要複製那段SQL語法就好了(通常都在最下面)。

CREATE TABLE [dbo].[__MigrationHistory] (
    [MigrationId] [nvarchar](150) NOT NULL,
    [ContextKey] [nvarchar](300) NOT NULL,
    [Model] [varbinary](max) NOT NULL,
    [ProductVersion] [nvarchar](32) NOT NULL,
    CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY ([MigrationId], [ContextKey])
)
INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'202002150837512_init', N'WebApplication2.Migrations.Configuration', ......')

並將複製的SQL語法,貼在 MS-SQL 的新增查詢並執行,MigrationHistory Table 就會出現了。

https://ithelp.ithome.com.tw/upload/images/20200215/20119925OrGRy8XfOI.png

接下來就若有修改 Model 類別等程式碼,就可以直接透過 EF Migrations 進行 Add-Migration 和 update-database 了

Add-Migration [修改記錄的名字]
Update-Database

就回到了 Code First 了,以上,感謝閱讀。


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1

感謝分享

0
thinkawait
iT邦新手 5 級 ‧ 2021-09-27 15:00:22

第7梯後端拜讀<3

0
umworksite
iT邦新手 5 級 ‧ 2021-11-21 22:36:56

感謝分享 先mark起來之後感覺會用上@@

我要留言

立即登入留言