EF Code First Change Tracking

Change tracking is a central concept for every Object-Relational Mapper, including Entity Framework. When doing updates to objects the normal work flow with Entity Framework has three steps.

  1. Retrieve data from the database.
  2. Update some properties on some objects.
  3. Save the updates to the database.

In the third step Entity Framework has to find out what properties on what objects were changed. There are two basic strategies to accomplish this.

  1. Keep a copy of each object’s original state and compare it with all loaded objects when saving updates to the database.
  2. Have a mechanism where the object context gets notified of changes to entity objects.

Using Entity Framework to Create a Database

It’s been a few years since I worked with Entity Framework and there have been substantial improvements. Still I have been missing a life cycle view on the database, where the evolution of a database schema during development and maintenance is fully supported. The latest Entity Framework 4.3 release contains EF Migrations which claims to solve the problem. In a series of post I will explore EF Migrations and see if it can do what I want. In this post I will focus on getting Entity Framework to create a database with a decent schema for me. If I succeed I will continue with the migrations tools to see if they are powerful enough for what I want. For the database I want to have a schema that contains the following features:

  1. Database generated int identity(1,1) primary keys.
  2. Foreign Keys
  3. Custom string length constraints.
  4. Indexes
  5. Table mapped to an enum in code.
Software Development is a Job – Coding is a Passion

I'm Anders Abel, an independent systems architect and developer in Stockholm, Sweden.

profile for Anders Abel at Stack Overflow, Q&A for professional and enthusiast programmers

Code for most posts is available on my GitHub account.

Popular Posts

Archives

Series

Powered by WordPress with the Passion for Coding theme.