Home > Blockchain >  Error connecting to SQL Server from Razor Pages
Error connecting to SQL Server from Razor Pages

Time:01-19

I am trying to convert my very basic website from straight HTML to Razor Pages. My main resources have been Adam Freeman and Mike Brind. So far I have created a small foundation and now am trying to run a query against my database in Index.cshtml. If I comment out the query, the application will compile and run (that is to say it will display the home page). When I try to include the query I get a runtime error when trying to load the home page:

An unhandled exception occurred while processing the request. SqlException: Cannot open database "Alpha" requested by the login. The login failed.

InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure' to the 'UseSqlServer' call.

Alpha.Pages.Pages_Index.ExecuteAsync() in Index.cshtml @foreach (YearSummary s in Model.YearSummaries) {

fail: Microsoft.EntityFrameworkCore.Database.Connection[20004] An error occurred using the connection to database 'Alpha' on server '(localdb)\MSSQLLocalDB'.

fail: Microsoft.EntityFrameworkCore.Query[10100] An exception occurred while iterating over the results of a query for context type 'Alpha.Data.AlphaContext'.

I'm assuming this has something to do with my connection string.

appsettings.json "AllowedHosts": "*", "ConnectionStrings": { "AlphaConnection": "Server=(localdb)\MSSQLLocalDB;Database=Alpha;MultipleActiveResultSets=true" }

Program.cs builder.Services.AddDbContext(options => { options.UseSqlServer(builder.Configuration.GetConnectionString("AlphaConnection")); });

Index.cshtml @foreach (YearSummary s in Model.YearSummaries) {

I've played around with the connection string, with no luck. I really don't know where to go from here.

I have uploaded my code to GitHub: enter image description here

Is this the effect of your page loading normally?

  • Related