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

Tracking and No Tracking in Entity Framework Core

Tracking vs No-Tracking queries

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.

Tracking can be disabled at a query level by adding AsNoTracking extension method to the query. For queries which retrieve large amount of read-only data it is recommended to disable tracking to improve performance. Disabling tracking helps us to gain some performance and also save on memory since we don’t have to save tracking information in the cache.

To disable tracking at the context level we need to set NoTracking to the QueryTrackingBehavior property as follows.

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.