Taming Entity Framework performance

Story by Abhilash on Entity Framework


Entity Framework (EF) is an object-relational mapper (O/RM) that enables .NET developers to work with a database using .NET objects. It eliminates the need for most of the data-access code that developers usually need to write.

Favorably, most developers prefer code-first approach because it allows to do database operations end-to-end through C# code - from creating tables, to querying data to performing CRUD operations. If you have extensively used EF for your projects, you know about the performance bottlenecks while querying and updating database contents. You may not get the same performance in database operations similar to what was offered by native implementations like ADO.NET or other techniques.

So how to gain performance without losing the flexibility and benefits offered by EF? The short answer is to off-load entire database operations to light-weight ORM like Dapper. Here is how the entire solution looks like.

  1. EF with code-first approach will be used to create tables, defining relationships (primary key, foreign key, etc.) and updating schema to database.
  2. Use micro ORM - like Dapper (https://github.com/StackExchange/Dapper) to manage read/write operations in database. This is blazing fast and is proven one. Dapper is being used in one of the high traffic website - StackOverflow (https://stackoverflow.com).

The ideal approach is to create wrapper around Dapper using C# Generics so it can be invoked from anywhere without dealing with boilerplate codes. Here is the Gist for performing database operation using Dapper. The code covers most of the use cases and can be readily used as-is - https://gist.github.com/abhilashca/6d5d73c6803789a5572d750e9b47a06a.

Hope this helps.

comments powered by Disqus

Related Story   →

Fix: HTML5 Video not playing when deployed to azure

A post on how to playback HTML5 video when published to Azure.