Posts

Showing posts with the label Entity Framework Core

Entity Framework Core Performance Optimization

Entity Framework Core Performance Optimization Last year I talked about Entity Framework Core . It is a easy and feature rich ORM which makes working with database in a .NET environment typesafe. But even though it makes things easy, there are ambiguous cases which can take us off guard. Today we will see four of this cases and how to deal with them. Client evaluation Iteration Include and ThenInclude NoTracking For the following examples, I will be using SQLite with Entity Framework Core. tl;dr Make sure that the query constructed in c# uses function that can be translated to SQL, Make sure that there isn’t an abnormal amount of queries created and that it does not iter item per item, Make sure to use Include and ThenInclude for object relation to include them after query execution, before query execution it is not needed, Use NoTracking for readonly queries to disable tracking on entity to yield better performance. 1. Client evaluation The following example illust...

Save array of string EntityFramework Core

Save array of string EntityFramework Core EntityFramework is a ORM where R stands for Relational (database). Relational databases operate with tables, rows and columns. If we have an object containing an array of objects, by default, it would translate to two tables and a JOIN between the two tables. This behaviour is great if our initial model has links to other objects but not so great when the array inside of model is only composed by primitive type values, like strings. Today we will see how we can store array of values as property of our model and prevent having to create two tables with a JOIN. 1. Install EntityFramework Core Sqlite 2. Save array of primitive type values All the code can be found on my GitHub . 1. Install EntityFramework Core Sqlite In this example I will be using Sqlite. We start by creating two projects, a .NET Core/ASP.NET Core project and a .NET Standard library project. In the library we install Microsoft.EntityFrameworkCore and in the web project we...

Saving data with Entity Framework Core with SQLite

Image
Saving data with Entity Framework Core with SQLite Entity Framework is a framework abstracting away all the complexity of dealing with storage. This abstraction is also known as ORM ~ object-relational mapping. There is a number of provider which are implementation of the storage like SQL server or MySql or also SQLite, the one we will be seeing in this post. SQLite is a embedded database. The whole database is contained within a single .db file which makes it highly portable, so portable that it is the default database installed in mobile OS like iOS and Android . It is extremely easy to use and to maintain. It also offer a powerful implementation of SQL . Today we will see how we can make use of Entity Framework with SQLite provider in a ASP.NET Core application. 1. Install EF and create a new DbContext 2. Create migrations 3. Use in ASP NET Core 1. Install EF and create a new DbContext Start by installing the packages: Install-Package Microsoft.EntityFrameworkCore.SQL...