iT邦幫忙

0

關於asp.net core 連線字串 connection string 是否要放入appsettings.json

  • 分享至 

  • xImage

目前遇到問題是我照著官方作法將connection string 放入appsettings.json 如下圖:
appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "ConnectionStrings": {
    "DBConnectionString": "Server=\\SqlExpress;Database=MVC_UserDB2.ConsoleApp.NewDb;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "AllowedHosts": "*"
}

並在stratup加入該設定
https://ithelp.ithome.com.tw/upload/images/20220830/20123630Sg3jMgydRm.png

但執行時會報錯!!
https://ithelp.ithome.com.tw/upload/images/20220830/20123630NUXggs9T3V.png

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

2 個回答

1
科科
iT邦研究生 1 級 ‧ 2022-08-30 22:41:15

改成這樣試試

"DBConnectionString": "Server=.\SqlExpress;Database=MVC_UserDB2.ConsoleApp.NewDb;Trusted_Connection=True;MultipleActiveResultSets=true"
0
緯大啊緯大人
iT邦研究生 1 級 ‧ 2022-08-31 08:46:10

感覺看起來不像連線字串問題

會是Context部分問題? 可以檢查一下MVC_UserDB2Context
你的錯誤訊息後面內容好像是
If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

我要發表回答

立即登入回答