iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 21
0
Software Development

在Mac上開發.Net Core系列 第 21

Day - 21 從零開始(五)

繼上一篇我們輕鬆完成db first之後,

這一次我們來開始code first吧

首先我們先建立Blog跟Post的類別,並幫他們做好關聯
https://ithelp.ithome.com.tw/upload/images/20180111/20103426pOcE5cdkp1.png

https://ithelp.ithome.com.tw/upload/images/20180111/201034262hy7a78gjc.png

再來建立Blogging的DbContext
https://ithelp.ithome.com.tw/upload/images/20180111/201034261Z8CNHQ3fH.png

接下來我們在Startup裡去註冊我們的db

我們在ConfigureServices裡面加入下面這一行

services.AddDbContext<BloggingContext>(options => options.UseSqlServer(連線字串));

最後再來就剩下CLI的事情嚕

首先我們先來看看這邊該怎麼做
https://ithelp.ithome.com.tw/upload/images/20180111/20103426k3RP7ZGnSi.png

發現migrations,這邊我們就繼續查下去
https://ithelp.ithome.com.tw/upload/images/20180111/20103426YUwxh5n7YJ.png

我這邊我們來查詢一下add的部分該怎麼做
https://ithelp.ithome.com.tw/upload/images/20180111/201034260YkAJ3TgQo.png

確認後就讓我們打入正確的指令以及參數
https://ithelp.ithome.com.tw/upload/images/20180111/20103426ADYVkGdMjj.png

Code First成功!!

接著讓我們來驗證一下

我們利用上一篇建立好的Blogs的Controller

在建構子裡面我們注入BloggingContext
https://ithelp.ithome.com.tw/upload/images/20180113/20103426qNrTWXgoz4.png

https://ithelp.ithome.com.tw/upload/images/20180113/20103426RG2rygdb0X.png

接著我們打開postman來get試試看
https://ithelp.ithome.com.tw/upload/images/20180113/20103426UFSQivuS6A.png

成功


上一篇
Day - 20 從零開始(四)
下一篇
Day - 22 從零開始(六)
系列文
在Mac上開發.Net Core30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言