Innovile
Published in

Innovile

Some Entity Framework Usage Methods

Greatings, in this article, I will be telling about a few methods that you can use in some operations using Entity Framework which one of the common ORM (Object Relational Mapping) tools. The methods I will discuss will answer some of the common questions in entity framework usage.

First of all, I want to talk about a subject that helps you with error detection or logging. For any select operation on the database, framework creates a corresponding sql for our request and it is possible to obtain this sql. To put it simply, it is possible to access the query of a “GetAll” or “Find” request that starts with “Select”.

Microsoft.EntityFrameworkCore.Query.SqlExpressions and Microsoft.EntityFrameworkCore.Query libraries should be used to perform the process I mentioned and the solutions in the next images were run on .Net Core 3.1.

We already obtained query pattern on “command.CommandText” but if you want to get query with filter values like “Id = 1 and Name = “Mike” “SubstituteVariables” aims to make this this with replacements.

After implementing those methods you can enjoy very simple usage:

var results =_userRepository.Find(x => x.UserId == id && x.UserName = userName));

var query = results.ToSql();

Secondly, when a new record will be added on the database, if the table contains auto increment sequence -generally Id column-, next piece of code returns a returnModel which keeps increment value of inserted entity on it’s Id. The rule in here, you should load ReturnModel.Id after “_dbContext.SaveChanges();”.

The third topic of the article is about Eager Loading, Lazy Loading and Explicit Loading in Entity Framework. These terms describe the methods we can apply according to possible usage scenarios in transactions on related entities.

Eager Loading is a method that you can bring in child entities with a single database call. This method is also the method in which the amount of queries to run on the server is the least. If you are sure that you will use related entities together with the main entity, it is recommended to use this Eager Loading. In the example below, you can see that the UserRole entity is brought together with the User relational entity at once with the include command.

In Lazy Loading, a child is loaded only the first time it is accessed. It delays the loading of relevant data until you request it. If you are going to work with one-to-many relationships or do not have to use the relevant data immediately, this method is preferable.

Entity Framework has the option to turn off Lazy Loading and if you need access to the children of an entity after closing it, you can use Explicit Loading. You can take advantage of this method when coding algorithms that you are not sure that you will need child entities before.

Thanks for reading…

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store