Showing posts with label Asp.Net Core. Show all posts
Showing posts with label Asp.Net Core. Show all posts

Friday, September 25, 2020

Building an ASP.Net Core Web App Docker image

 In this post we shall see how to create a basic Asp.Net core Hello World App and package it into a Docker image. We will deploy the image as a Docker container and execute the Asp.Net Core application from Docker.

First let us start by creating an Asp.Net Core application using Visual Studio 2019

Friday, April 10, 2020

Config settings in ASP.NET Core Overview

Access config settings directly from class library

In the previous post we saw on how to access the config settings by creating a custom class and injecting it in the startup, so that it can be used in the controllers. In this case the startup and the controller classes are in the same project.

Access config settings using custom class

In the previous post we saw on how to use Dependency Injection to get the configuration options in the Controller classes. In this post we will see another way of reading the configuration setting using a custom class.

Access config settings from appsetting.json in Controller using DI

In the previous post we saw the options to read config setting values in Startup, in this post we shall see on how to get the config setting values in the controller. We will inject the configuration object to the service and the same will be made available in the Controller using Dependency Injection.

Thursday, April 9, 2020

Access configuration settings from custom configuration files

In the previous post we saw on how to read configuration settings from the appsettings.json file. While this holds good for most of the cases, there might be situations where we will need our custom configuration file, when we use our own configuration file, we need to specify the file in the Startup and add the file to the ConfigurationBuilder.

In the below case we have a custom configuration file mysettings.json, and we will read the configuration settings from our config file.

Access configuration settings from appsetting.json

We saw about the various configuration options provided by .Net Core in the previous post. In this post we shall see on how to use the configuration file option to store and retrieve key-value configuration settings.

Traditionally we are used to store configuration settings in environmental variables or config files like .INI files, web.config files etc. Asp.Net core provides a default JSON based configuration file called appsettings.json file. In this post we shall see on how to read configuration settings from the appsettings.json file in the application startup.

Asp.Net Core Configuration

Configuration settings is an important part of any large application. We will have to store and use various config settings like connection string, file path etc in an application. In the older version of Asp.Net Applications configuration settings were stored in web.config files which is an XML file. The System.Configuration namespace was used to read key-value pairs from XML configuration file web.config.

Asp.Net Core provides various options to store and retrieve configuration settings. Following are some of the options provided by Asp.Net Core to store and retrieve configuration settings.

Monday, April 6, 2020

Asp.Net Core Logging Overview

Asp.Net Core Console logging

In the previous post we saw the various Interfaces and Classes provided by Asp.Net Core to support logging, in this post we shall see on how to enable Console logging in an Asp.Net Core Web API application.

To enable a specific type of log we need to add the log provider to the startup. To add a provider we need to call the provider's Add extension method under ConfigureLogging in Program.cs. To Add a console log provider we need to add the following to the Program.cs

Asp.Net Core Logging

Logging is a crucial part of any application, especially for large applications logging is very important in production environment where debugging is not straight forward. The Asp.Net Core framework provides built-in support for logging.

In Asp.Net Core logging support is provided by the Microsoft.Extensions.Logging namespace. This namespace provides the following classes and interfaces to support Logging.

SQL Queries in Entity Framework Core

Combining SQL Queries with LINQ Queries

We have seen different ways of executing LINQ queries and ways to execute SQL queries from EF Core DBContext in isolation, now we shall see on how to combine SQL Queries with LINQ queries and execute them as a single query.

Let us get the list of Employees from the Employee table using SQL query and use the Include extension method to get the related department details as follows.

Executing Stored Procedures in EF Core

In the previous post we saw on how to execute SQL Queries from EF Core context, in this post we shall see on how to execute stored procedures using EF Core. Let us use the same Employee table, and create the following stored procedure to get the list of employees.

Executing SQL Queries in EF Core

So far we have seen different ways of writing LINQ queries using Entity Framework Core, but sometimes we cannot express certain complex scenarios as LINQ queries or the LINQ query might not be as efficient as the SQL query, in those cases we will have to execute SQL queries directly.

Entity Framework Core allows us to execute SQL queries against the underlying database with some restrictions. To execute SQL queries directly we need to use the FromSql extension method with the context object as follows.

Friday, April 3, 2020

No-tracking queries in Entity Framework Core

Entities retrieved using Entity Framework Core have tracking enabled by default, however there are some situations in which we will not need tracking. Let us say we need to load a Grid in the UI with a large amount of data which will not be updated in this case we will not need tracking since the data is read-only.

Tracking queries in Entity Framework Core

Entities retrieved using Entity Framework Core LINQ queries have tracking enabled by default, this allows us to make changes to the entity and save them back to the database using SaveChanges. When we execute a query in Entity Framework Core the resultant object is stored in a cache which allows us to make changes and use the object to update the database.

Monday, March 30, 2020

Lazy loading Performance issues

Lazy loading allows us to reduce the size of the Main entity object in a LINQ query which has references to related entity objects. When the query executes it fetched the data of only the main entity object, data for the related objects are loaded only when we access the Navigational property from the main table.

With this approach we might end us firing multiple SQL queries to the underlying database and eventually bring down the performance. Let us assume we have a Grid in the View which displays a list of employees and each item has a details link which will display employee data and its related entity department’s data, then when the page loads a SQL query related to the employee’s entity will execute and get the list of employees. Later when the user clicks on each employee details link separate SQL queries will get executed to get each employees department data as follows.

Tuesday, September 24, 2019

EF Core joining tables using Include and selecting specific columns

In the previous posts we saw on how to join tables using Include and how to select specific columns form a table. In this post we will try to combine both of them, we will join tables using Include and select specific columns from the joined tables.

We will join the Employee table with Department and Country tables, select specific columns from each of the table and populate the below Model class.