Category Archives: Entity Framework

Unit Testing while using Entity Framework

I have been recommended to use Entity Framework (EF) instead of LINQ to SQL while building .NET applications. So I pulled out some reference materials, learned the beginning concepts of EF, and started to build an application.

Then I started trying to figure out how to do Unit Testing and Test Driven Development (TDD) while using EF.  What I had read about EF showed me how to build a SQL Server database and then have Visual Studio build the entities for me. I had to design the entire database first and then leave all the classes tied to the database, both of which are incompatible with TDD and Unit Testing.

So I started my Google search, and these are the documents I found really helpful. In the end, Unit Testing and TDD are compatible with EF!  Here is where my journey took me:

TDD and ADO.NET Entity Framework
This late 2008 posting has several people sharing their concerns about using TDD with EF, which is where I was yesterday morning.

Walkthrough: POCO Template for the Entity Framework
Feature CTP Walkthrough: POCO Templates for the Entity Framework
These articles shows how to use the  ADO.NET C# POCO Entity Generator and the Microsoft ADO.NET Entity Framework Feature Community Technology Preview 3 assembly to separate a .edmx Entity Framework file into two groups of classes: the persistence ignorant entity classes (not connected to the database), and the data context class which talks with the database.

Feature CTP Walkthrough: Code Only for the Entity Framework (Updated)

This posting illustrates how to build an EF from code only, without using Visual Studio’s automated generators.  In this process, they were able to keep the persistence ignorant entity classes separate from the database context classes.  This technique required the Microsoft ADO.NET Entity Framework Feature Community Technology Preview 3 assembly.
These articles illustrate how to practically use TDD and EF together!
Now I feel prepared to move forward using Entity Framework together with Test Driven Development methodologies!