Click here to Skip to main content
13,002,399 members (77,954 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 5 Dec 2010

How to Separate Self-Tracking Entities to Their Own Class Library

, 5 Dec 2010
Rate this:
Please Sign up or sign in to vote.
How to Separate Self-Tracking Entities to Their Own Class Library


Last week I had a session in Teched Israel 2010 which included approaches for building N-Tier applications on top How to Separate Self-Tracking Entities to Their Own Class Libraryof Entity Framework 4 as the data access technology. During the session, I showed an example of how to use the new Self-Tracking Entities (STE) feature. In this post, I‘m going to show how easy it is to put the STE in a different class library as a jump start for using this T4 Template in N-Tier scenarios.

What is Self-Tracking Entities?

In EF4, a new feature was introduced – the Self-Tracking Entities (STE). The STE are simple entities that track their own changes instead of EF’s ObjectContext. Every entity implements the IObjectWithChangeTracker interface and has a ChangeTracker property that exposes the tracking mechanism. Using STE enables you to use EF in N-Tier scenarios and gain the ability to track changes on the client side. This of course indicates that we will need a dependency on these entities on the client side (and that we will lose interoperability). In order to do that, you will need to separate the entities from the data access layer and to put them in their own class library.

Separating Self-Tracking Entities to Their Own Class Library

You start with two empty class libraries – one for he data access layer and the second for the entities that we are going to generate:

Empty Class Libraries

First, create your Entity Framework model in the STEDAL class library. In the EF designer surface, use the Add Code Generation Item in order to pick the Self-Tracking Entities T4 Template from the T4 Template options:

Add Code Generation Item

Self-Tracking Entity Generator

Now the STEDAL will look like:

After using STE T4 template

Pay attention that you have two different tt files. The first one ( holds the context and the second one ( holds the entities. The reason for the separation is obvious – to enable you to move the entities to their own class library.
The next step is to cut and paste the entities to the STEEntities class library. When you do that, pay attention that you will need to add a reference to System.Runtime.Serialization since the STE use WCF data contracts for serialization. Also, you will need to add a reference from the STEDAL to the STEEntities.

STE Class Library

Now we need a simple fine tuning in the T4 template itself. Open the T4 template and at the head of the template locate the following line:

string inputFile = @"SchoolModel.edmx";

This line indicates where is the location for the edmx file. You will want to change it to a relative path to the real location of the edmx file for example:

string inputFile = @"../STEDAL/SchoolModel.edmx";

The reason for doing that is that the STE needs the Entity Data Model in order to work properly when we do data access operations.

The last step in the process is to put the context (or the entities if you prefer the opposite) in the same namespace as the entities. This can be achieved by using the file Custom Tool Namespace property. Put the STEEntities there and you are finished:

Custom Tool Namespace

Now you can build the solution and start using the libraries.


Let's sum up, when you want to use the new Self-Tracking Entities feature, the first thing to do will be to move the entities to their own class library. The process is very simple and in this post, I showed you how to do exactly that.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website:

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
shady197930-Dec-12 7:20
membershady197930-Dec-12 7:20 
GeneralRe: My vote of 5 Pin
Gil Fink30-Dec-12 19:08
memberGil Fink30-Dec-12 19:08 
GeneralMy vote of 5 Pin
SvenVdb15-Jun-11 11:44
memberSvenVdb15-Jun-11 11:44 
Generaldoes not work Pin
Member 10419916-Apr-11 18:35
memberMember 10419916-Apr-11 18:35 
GeneralRe: does not work Pin
Gil Fink16-Apr-11 20:44
memberGil Fink16-Apr-11 20:44 
GeneralRe: does not work Pin
SvenVdb15-Jun-11 11:45
memberSvenVdb15-Jun-11 11:45 
GeneralRe: does not work Pin
Mopthe7-Aug-12 3:05
memberMopthe7-Aug-12 3:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170626.1 | Last Updated 5 Dec 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid