ASP.NET Core

ASP.NET Core MVC アプリケーションでデータベースマイグレーションを行う。

2020年7月3日

デフォルトの状態では、アプリケーション内で登録されている接続先データベースは生成されていません。接続先データベースは appsettings.json に記述されています。

appsettings.json の例

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-App;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

そして、ユーザー登録など、データベース接続を行おうとすると、次のエラー画面が表示されます。

このような場合、パッケージマネージャーコンソールで、Update-Database を実行します。

PM> Update-Database

-ASP.NET Core