The article provides a quick guide to understanding Entity Framework.

Why Entity Framework?

Entity Framework is an ORM layer which implements the Unit of Work pattern. The library keeps a track of all changes made to database entities. It saves all changes in a single transaction.

DbContext class keeps track of the entities. The SaveChanges method saves multiple updates in a single transaction.

DbSet<T> represents an entity collection. Perform database updates using Add and Remove methods. Retrieve data by executing queries against a DBSet using LINQ.

Flavours of Entity Framework

Entity Framework has three flavours: Database first, Model first, Code first. There are three parts to Entity Framework: Database schema, EDMX model and .NET classes.

  • Database first approach: Specify the database schema first. Generating the model and classes.
  • Code first approach: Specify the classes. Generate the model and database schema.
  • Model first approach: Specify EDMX model. Generate the database and classes.

Code First

I prefer Code First approach. In this approach, we code-up all the entity classes. The code has the mapping information.

  • Map table name to class name.
  • Map column name to property name.
  • Set a property as primary key.
  • Set a property as identity column.
  • Specify Foreign key relations.

There are two ways to represent the mapping: Data annotations or Fluent API. Data annotations uses C# attributes to represent the mapping. Fluent API, on the other hand, uses the OnModelCreating method of DbContext to write the mapping code.

Database Migrations

While initialising Entity Framework, we check if database schema needs to be updated. Database schema updates as part of initialisation is called migrations.

When DbContext is first called, it creates a new database, if no database exists. Within the database, the initialiser creates a __MigrationHistory table. The EDMX model for the database is stored in the Model column of the Migration history table.

If the database already exists, the initialiser retrieves the Model from __MigrationHistory. If there is a discrepancy between the current model and the model stored in __MigrationHistory, the initialiser prompts for a migration.

There are two types of migration: Automatic Migration or Manual Migration.

I prefer automatic migration. For most cases, Automatic migration works. The initialiser knows how to make the database update. But in some cases, we need to do the migration ourselves. The update-database command in NuGet package console performs manual migrations.

Complex schema updates makes migration difficult. There are various database initialisers available. It is also possible to drop and recreate the database instead of applying incremental updates. Any built-in initialiser has a Seed method. Apart from applying schema updates, the Seed method initialises the database with data.

In production environment, we do not want migrations to happen. Migrations can be disabled using a custom initialiser or null initialiser which does nothing.


Quick guide to learning Entity Framework
Tagged on: