What is the Cartesian Explosion in EF Core?

An entity framework is an ORM that makes developer’s lives easier. Using EF, we can interact with the database and perform CRUD operations using a programming language (C#, VB.net). In this blog, we are going to discuss the cartesian explosion in regards to loading data.

Cartesian Explosion

It is related to performing joins. When you perform a join, one table’s columns repeat the X number of times of the matched records from the joined table.

var customers = context.customers
.include(a => a.address)
.ToList();

The LINQ query in SQL:

Select * From Customers
 Left Join Address 
 on Address.Id = Customers.AddressId
Customer IdAddress Details
11
12
…..…..
1500

You can see, the columns of the customer table are repeated 500 times. Now, imagine if you are working on a complex dataset where you have to add a few more includes. The returned data set would explode – it is known as the cartesian explosion.

on-to-the-next-project

Solution

The solution to this is that you split the queries and get data two data sets, rather than one. It would result in two database round trips but would improve performance (fewer reads and CPU time)

var customers = context.customers.ToList();

var addresses = context.addresses.ToList();

Conclusion

In this blog, we discussed what is the cartesian explosion in EF’s eager loading data pattern. You have to be very careful about when to avoid this problem. It is recommended to use the split query approach when the dataset is too large and requires optimization. Otherwise, in normal scenarios include would do the job.

Docker Compose with Asp.Net Core Microservice

In the last blog, we learned about the core concepts of Docker and how we can create a run .net core app in the container. Today, we are going to learn about the docker-compose to run more than one container together.

Docker-compose file

It is a YAML file. By default, it’s named docker-compose.yml but it can be changed and specified as a parameter.

First of all, we specify the version of the file. This should be the latest version. I am using 3.6.

Next, we define our set of services. This is where the magic happens. We can define multi-services in one file and run all the service with a single command.

Service has a name, image, build context, environment, network and volume. You can see all the options here.

Docker-compose commands

It’s very easy to manage dockers with docker-compose commands. There are two main commands. Docker-compose up / Docker-compose down.

Docker-compose up

docker-compose up -d –build

Docker-compose up command run all the services in the compose file. You need to run this command from the directory where docker-compose .yml file is placed.

-d paramter is used to detach console from the container.

By default, images are built when you run this command for the first time. If you make changes to the code, you need to use the –build parameter to tell docker to build image(s) again.

Docker-compose down

docker-compose down

Docker-compose down command stops/removes all the services defined in the compose file. You need to run this command from the directory where the docker-compose .yml file is placed.

Conclusion

Docker-compose is very simple, and helps us run multi-container services from a single file. It is very handy and powerful.

You can download the reference source code on my Github.