为什么我在添加迁移后无法访问页面,即使一切都已成功构建?

Why I cannot access the page after adding a migration, even if everything built successfully?

我想构建一个网络应用程序,其中包含一个页面,我可以在其中为教授分配课程 - 我已经使用 Identity API 创建了 login-logout/register 页面 - 为了做到这一点,我使用 Razor Pages Entity Framework (CRUD)。我之前已经进行了一些迁移,我想添加另一个名为 'InitialCreate' 的迁移。我在数据包管理器控制台中输入了以下命令:

Add-Migration InitialCreate -Context ApplicationDbContext

Update-Database -Context ApplicationDbContext

一切构建成功,但是当我 运行 代码并尝试访问页面“AlocareMaterii”时,该页面使用通过我的本地主机 (localhost:x/AlocareMaterii) 进行的迁移,我收到以下错误:

A database operation failed while processing the request.
SqlException: Cannot open database "razor_licenta_runtimeContext-a7b890ea-d1ed-4faa-afdf-88dcf8d382d4Trusted_Connection=True" requested by the login. The login failed. Login failed for user 'DESKTOP-3AH99PG\ninex'.
Use migrations to create the database for razor_licenta_runtimeContext
In Visual Studio, use the Package Manager Console to scaffold a new migration and apply it to the database:

PM> Add-Migration [migration name]
PM> Update-Database
Alternatively, you can scaffold a new migration and apply it from a command prompt at your project directory:

> dotnet ef migrations add [migration name]
> dotnet ef database update

这是我的 'appsettings.json' 文件:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=aspnet-razor_licenta_runtime-53bc9b9d-9d6a-45d4-8429-2a2761773502;Trusted_Connection=True;MultipleActiveResultSets=true",
    "razor_licenta_runtimeContext": "Server=(localdb)\mssqllocaldb;Database=razor_licenta_runtimeContext-a7b890ea-d1ed-4faa-afdf-88dcf8d382d4Trusted_Connection=True;MultipleActiveResultSets=true"
  },

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

首先,我认为您的问题是“razor_licenta_runtimeContext”,您应该使用 add-migration initial -context razor_licenta_runtimeContext 进行迁移,update-database -context razor_licenta_runtimeContext 我认为它会解决问题。