Turn off the GDPR cookie notification in a dotnet core MVC web application

The dotnet core MVC web application template adds the GDPR cookie acceptance message to the header of the page template. When the site is hosted with HTTP the accept button does not clear the message resulting an infinite loop, when the user cannot log in and the site continuously asks for the acceptance. When the […]

Read the database connection string and other secrets from environment variables

It is not good practice to store secrets in the source code repository. The sample C# dotnet core application code stores the database connection string in the appsettings.json file which eventually will be pushed into GitHub. To follow the Twelve-Factor App methodology, store the configuration values in environment variables and load them runtime. This method […]

Set the environment for an ASP.NET Core MVC web application

When the ASP.NET Core MVC web application starts, reads the name of the environment for an environment variable of the host computer. The compiled application contains the configuration for all supported environments, making possible to build and deploy the same artifact in every environment. If the ASPNETCORE_ENVIRONMENT environment variable is not defined, the Production environment settings are […]

Enable .NET Core Entity Framework Linq query execution

When we create a new .NET Core class it only contains the using System; reference. To enable Linq database queries in a .NET Core project, add using System.Linq; using System; using System.Linq; To enable the usage of .Include() in the Linq queries, add using Microsoft.EntityFrameworkCore;  

Return values to the controller in the model from the ASP.NET MVC view

When we create an ASP.NET MVC web application, the model (an object) is the easiest way to return values from the view to the controller. For the view to be able to return the values in the model, make sure the model contains properties, not fields, for every value with { get; set; } public class MyViewModel { public […]

Reverse engineer a database with AspNetCore in Visual Studio

For some reason the .NETCore designers did not think, that developers want to follow best practices by separating the data layer from the presentation layer. The Entity framework out of the box only works if the database is accessed from the main application project. When we try to reverse engineer a PostgreSQL database from a […]

The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

When I made changes to the web.config file of an ASP.Net C# application, I have accidentally deleted a comma, and I started to get the runtime error message: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) Make sure all necessary commas are there in the config files, Visual Studio does not […]

{“error”:”Entity type ‘AspNetUserRoles’ has composite primary key defined with data annotations. To set composite primary key, use fluent API.”}

When a database table has composite keys (multiple columns are in the key) you cannot use the usual key definition [Key] [Display(Name = “UserId”)] public string UserId { get; set; } [Key] [Display(Name = “RoleId”)] public string RoleId { get; set; } When you run the application you get the error message: {“error”:”Entity type ‘AspNetUserRoles’ has composite […]

Generate SQL script from Entity Framework migration script

It is not recommended to execute Entity Framework migration scripts in production, It is important that you execute all SQL steps manually on the production database section-by-section to immediately see the result and be able to recover in case a destructive action goes wrong. The Visual Studio .NET application templates contain Entity Framework migration scripts to […]