Error connecting to SQL Server from Razor Pages - asp.net-core

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: https://github.com/JB45424/Alpha
Thank you for any assistance.

Have you updated the database after the database migration? If the database is not updated, problems like this can occur.
I cloned your code and ran it directly and I also encountered your error. But after I install the Microsoft.EntityFrameworkCore.Tools NuGet package and run the following commands to migrate and update the database, the program works fine:
Add-Migration InitialCreate
Update-Database
Helpful link: Test the Asp.Net Core MVC app.
Test Result:
Is this the effect of your page loading normally?

Related

UMBRACO 10 HTTP Error 500.30 - ASP.NET Core app failed to start

I am trying to deploy my Website with umbraco 10, I always get the following error:
HTTP Error 500.30 - ASP.NET Core app failed to start
The website works correctly locally, however when deploying the app I get the same error...
Details:
DB SQL Server
Azure WebApp .Net6
What i have tried
I have followed the instructions: https://our.umbraco.com/documentation/Fundamentals/Setup/Server-Setup/azure-web-apps
I have created a project from scratch, thinking that the problem was some package, but still the same error.
This is how my umbraco 10 application looks once deployed from VS to an appservice (Azure)
The first thing I would do is check the logs on the the environment where you have deployed the code. That should give you a better idea what is actually going wrong as the error you are getting is very generic and not that helpful.
I have seen issues where the media folder is missing and that is something to check.
When I have issues in Azure, I also look at the files via the Azure Portal and check the connection string and any other configurations files to ensure they look correct. It could be the deployment has screwed up some values.
Have a look at this post which may provide some further details - https://our.umbraco.com/forum/using-umbraco-and-getting-started/108288-getting-http-error-50030-after-successful-publishing-to-azure

Microsoft Graph snippets sample for ASP.NET Core 3.1 - Authentication error

As far as I know I have followed the instructions for setting up the Microsoft Graph sample https://github.com/microsoftgraph/aspnet-snippets-sample, including joining the Microsoft 365 Developer Program. However when I run the sample and attempt to log in with my new Developer Program user I get the following error message:
Authentication error "Value cannot be null. (Parameter 'value')"
The error us being caught in this routine:
options.Events.OnAuthenticationFailed = context => {
var error = WebUtility.UrlEncode(context.Exception.Message);
context.Response
.Redirect($"/Home/ErrorWithMessage?message=Authentication+error&debug={error}");
context.HandleResponse();
return Task.FromResult(0);
};
If I log in with my personal Microsoft account then everything works fine, so I'm guessing this has something to do with my Developer Program account. The error message isn't very helpful and there not stack trace to speak of. I've tried using Fiddler to see if there's any more information but with no luck either. Any ideas about what I might be doing wrong?
Tested and reproduced the issue. You are getting this error because, as you have created application in one tenant and accessing the application with the Developer Program account(another tenant).
The Solution would be provide Provision for the application (service principal creation) Consent the application with the developer Program account(needs to be admin) if you want to use the application in developer tenant and Please below call
https://login.microsoftonline.com/common/oauth2/authorize?response_type=code&client_id=id&redirect_uri=https%3A%2F%2Flocalhost%3A44307%2F&prompt=admin_consent
After looking at the error messages in the output from the Kestrel web server I discovered that the line causing the problem was this one:
identity.AddClaim(new Claim(GraphClaimTypes.TimeZone, user.MailboxSettings.TimeZone));
and it was because the value of user.MailboxSettings.TimeZone was null. Once I set up a TimeZone in my developer account then everything works fine.

Publish .Net Core app with Entity Framework to IIS

I am trying to published my .Net Core app to IIS that uses Entity Framework. I can publish it fine but no database is included in the publish wizard.
I have followed different tutorials including this:
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-3.0
However none tell you how to deploy your database or what needs to be done if using entity framework. I'm relatively new to published and hosting web apps so I'm not sure what to do.
At the moment my frontend of the web app loads on IIS but when I go to login it brings up a 500 error. I have included my connection string and added a user and gve correct permissions under SSMS.
It came to my attention when publishing it shows "no databases found int he project".
Would this effect me not being able to access the database and bringing up the 500 error when logging in and how do i fix this.
No databases found in the project
How did you created your database locally in the first place? Did you manually ran the database update command? I would suggest you add to your startup.cs file a code to ensure your database is created and any missing migrations were applied, you can achieve this within your Configure method:
using (var scope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
scope.ServiceProvider.GetRequiredService<DbContext>().Database.Migrate();
}
Or just EnsureCreated() instead of Migrate() if you don't to apply missing migrations on future loads.
It seems that you need to change your ConnectionString configuration in appsettings.json from the format
"Data": {
"DefaultConnection": {
"ConnectionString": "xxxxxx"
}
}
to:
"ConnectionStrings": {
"DefaultConnection": "xxxxxx"
}
And your startup with
services.AddDbContext<ApplicationDbContext>(opt => opt.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));

You are trying to access item that no longer exists. The most probable reason is that it has been deleted by another user sitefinity

I have uploaded my new sitefinity from my local to my server but when I tried to login but its just showing error:
Server Error in '/' Application.You are trying to access item that no longer exists. The most probable reason is that it has been deleted by another user.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: Telerik.Sitefinity.SitefinityExceptions.ItemNotFoundException: You are trying to access item that no longer exists. The most probable reason is that it has been deleted by another user.
Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
I am new to sitefinity. Everything is working fine on my local but while on server its creating the issue. I am working on sitefinity 7.0.
http://www.sitefinity.com/documentation/documentationarticles/installation-and-administration-guide/deployment
Make sure you have completed all the steps.
Following the link in the answer above (https://stackoverflow.com/a/24241856/55718)
The main step I needed to do was make App_Data be controllable by the AppPool user. Then I had to rebuild the solution. This is on version 13.3, so still an issue overall but well documented and showed I just didn't follow the rules given.

local work but hosting on AppHarbor can not query SQL Server

So I have my web.config connection string pointing to my SQL Server on appharbor - from local (running Visual Studios) I can login and the memberships works. However from the build that is on the hosting side I get sorry, an error occurred while processing your request.
This is strange because I made a new user from local and check with SQL Server Management Studio whether or not the data has been added to the hosting server and it WORKS.
From my latest build I get the error:
Sorry, an error occurred while processing your request.
which is weird because they both point to the same server...
Anyone have an idea of what I am doing incorrectly?
ASP.NET MVC project has the "HandleErrorAttribute" on by default. You should remove it from FilterConfig.cs in App_Start Folder to see the actual error rather than the handled response.
It's located in the RegisterGlobalFilters method where filters.Add(new HandleErrorAttribute()); should be removed.
AppHarbor has a FAQ to help you troubleshoot deployment errors like these. In particular, you should inspect the "Errors" interface on AppHarbor and disable customErrors. Please elaborate the question if the FAQ doesn't help you solve the problem.