目前遇到問題是我照著官方作法將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加入該設定
但執行時會報錯!!
改成這樣試試
"DBConnectionString": "Server=.\SqlExpress;Database=MVC_UserDB2.ConsoleApp.NewDb;Trusted_Connection=True;MultipleActiveResultSets=true"
感覺看起來不像連線字串問題
會是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.