Click here to Skip to main content
15,664,272 members
Articles / Desktop Programming / WPF
Posted 7 Dec 2011


54 bookmarked

Building WPF Applications with Self-Tracking Entity Generator - Project Setup

Rate me:
Please Sign up or sign in to vote.
4.80/5 (11 votes)
20 Feb 2012CPOL10 min read
This article describes the project setup of building a WPF sample application with Self-Tracking Entity Generator for WPF/Silverlight.

Image 1



In this article, we introduce a new Visual Studio 2010 Extension called "Self-Tracking Entity Generator for WPF/Silverlight", and we will look into a demo application SchoolSample that was built with this Extension. Before we start, let us first take a look at the Extension's main features:

  • Auto-generate IClientChangeTracking interface implementation for all entity classes that provide client-side change tracking through each entity object. The interface includes methods and properties such as AcceptChanges(), RejectChanges(), HasChanges, and GetObjectGraphChanges() etc.
  • Auto-generate IDataErrorInfo interface implementation for all WPF entity classes and/or auto-generate INotifyDataErrorInfo interface implementation for all Silverlight entity classes.
  • Auto-generate display and validation attributes created by Portable Extensible Metadata for Entity Framework, along with an infrastructure for adding custom validation logic through source code.
  • Optionally auto-generate IEditableObject interface implementation for all entity classes that provide functionality to commit or rollback changes to an object that is used as a data source.

With these auto-generated functionalities such as client-side change tracking and multiple levels of validation support, along with authentication and authorization through Windows Identity Foundation (WIF), we should be able to build WPF LOB applications much more quickly.

This, however, does not mean that we can develop any type of WPF application with self-tracking entities. First, using self-tracking entities usually means that we need to develop both client and server assemblies with Microsoft .NET 4.0 and beyond. For non-.NET clients, it is a big obstacle to create change-tracking behaviors and consume WCF services built with self-tracking entities.

Another limitation is that we need to share the full entity classes on both client and server sides. In cases where we do not want to send all of an entity's properties to the client, we may have to develop an application with part of the data model using self-tracking entities, and the remaining part using DTO classes.

The Demo Application

The demo SchoolSample is a WPF application built with MVVM (MVVM Light Toolkit), Self-Tracking Entity Generator for WPF/Silverlight, and MEF. This simple application allows users to add/delete/update a student, an instructor, or a course and its enrollments. The Save button saves changes to the currently selected item, and the Cancel button cancels any changes made to that item. The Save All button loops through the whole list and saves changes for every item, and the Cancel All button loops through and cancels changes for all items. This sample does not show how to do authentication and authorization using WIF. For information about WIF, you can check Vittorio Bertocci's book Programming Windows Identity Foundation.

System Requirements

In order to build the demo application, we need:

Supported Operating Systems:

  • Windows Vista SP2, Windows Server 2008 SP2, Windows 7, or Windows Server 2008 R2
  • Windows XP SP3, or Windows Server 2003 R2 SP2 with special instructions below

Other requirements:

  • Microsoft SQL Server 2005, 2008, or 2008 R2
  • Microsoft Visual Studio 2010 Professional SP1
  • Self-Tracking Entity Generator for WPF/Silverlight (VS2010 Extension)
  • MVVM Light Toolkit V3 SP1 (included in the sample solution)
  • (Optional) Tangible T4 Editor plus modeling tools for VS2010 (VS2010 Extension)


After downloading the demo application source code on your local disk, we need to complete the following two steps:

Installing VS2010 Extension

First, we need to download the Self-Tracking Entity Generator for WPF/Silverlight Setup from the Downloads section of the project site. Extract its content, and run the installation package.

Image 2

This Visual Studio Extension Installer adds two Extensions to Visual Studio 2010 SP1, and you can verify that by going from "Tools" -> "Extension Manager..." as shown below.

Image 3

Image 4

The first Visual Studio Extension, C# Self-Tracking Entity Generator for WPF and SL, is a C# Entity Framework project item that generates self-tracking entity classes for WPF/Silverlight applications. The other Extension, Portable Extensible Metadata, is a Microsoft free download which you can also get separately from here.

Installing the Sample Database

To install the demo database, please run the SQL script School.sql included in the download source code. This script creates the SCHOOL database schema needed for our demo application.

Image 5

Building and Running the Sample

After completing the two installation steps above, we are now ready to start building and running the demo application. Please double check that the connectionStrings of the Web.config file in the project SchoolSample.Wcf is pointing to your newly created database. Currently, it is set as follows:

<add name="SchoolEntities" connectionString="metadata=res://
provider connection string=&quot;data source=localhost;initial catalog=SCHOOL;
integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" 
providerName="System.Data.EntityClient" />

When the demo compiles and runs successfully, we should be able to see this WPF application running like in the screenshot below:

Image 6


Solution Structure

Inside the demo's solution file, projects are organized into several solution folders. The Client folder comprises all the projects that eventually build and run as client-side WPF demo applications. While the Server folder consists of all the projects that provide WCF Services and run inside a web server environment. Next, let us briefly go over the main functionality of each project:

Image 7

For the projects inside the Server folder:

  • Project SchoolSample.Data.Wcf contains the server-side auto-generated data access entity classes, and any custom validation logic for each entity class.
  • Project SchoolSample.Wcf is the main server-side project with WCF Service classes, which query and update the SCHOOL database through the data access entity classes from the project SchoolSample.Data.Wcf.

For the projects inside the Client folder:

  • Project SchoolSample.Common contains all the common classes and interfaces that are shared among other client projects.
  • Project SchoolSample.Data is the client-side counterpart of project SchoolSample.Data.Wcf, and has the auto-generated self-tracking entity classes, and any custom validation logic for each entity class.
  • Project SchoolSample.WCFService contains the service proxy class for the class SchoolService from project SchoolSample.Wcf.
  • Project SchoolSample.Model defines the class SchoolModel that is needed for all ViewModel classes of this demo application.
  • Project SchoolSample.ViewModel keeps all ViewModel classes as follows:
    • MainPageViewModel
    • StudentPageViewModel
    • InstructorPageViewModel
    • CoursePageViewModel
  • Project SchoolSample is the main client-side project, and also hosts all the UI logic.

Project SchoolSample.Data.Wcf

Next, let us take a closer look at the server-side data access layer project SchoolSample.Data.Wcf.

Image 8

The folder EntityModel hosts the ADO.NET Entity Data Model EDM file along with three T4 template files created by the Self-Tracking Entity Generator for WPF/Silverlight. And, the folder Validation includes all custom validation logic defined on entity classes.

To add the three T4 template files, we need to first open the file SchoolModel.edmx, and then right-click and select "Add Code Generation Item...".

Image 9

Next, we will be prompted with the "Add New Item" dialog box. Select "Self-Tracking Entity Generator for WPF/Silverlight", type "" in the Name field, and hit the Add button.

Image 10

This will lead us to the second dialog box where we need to enter the entity class namespace "SchoolSample.EntityModel", choose whether to optionally generate the IEditableObject interface, and whether to generate code for WPF, Silverlight, or both. After clicking OK, three T4 template files will be created, and the T4 template files will auto-generate all the self-tracking entity classes based on SchoolModel.edmx.

Image 11

Project SchoolSample.Data

Project SchoolSample.Data contains the client-side auto-generated self-tracking entity classes and any custom validation logic for each entity class.

Image 12

The first thing to do for this project is to set a conditional compilation symbol WPF for all configurations, as shown below:

Image 13

The folder EntityModel hosts links to the files and from the project SchoolSample.Data.Wcf, and the folder Validation has links to all the files under the same Validation folder of the project SchoolSample.Data.Wcf. We can easily add a link as the following picture illustrates:

Image 14

One last note on these two client-side T4 template files is that whenever there are changes in the file SchoolModel.edmx, make sure to click "Run Custom Tool" so that the client-side entity classes would get updated as well.

Image 15

Setup on Windows XP

Unfortunately, Self-Tracking Entity Generator for WPF/Silverlight cannot be installed on Windows XP machines because of folder name length limitations. To build WPF applications of the same capability on Windows XP, we have to do a few steps manually. First, here is the list of requirements:

  • Microsoft SQL Server 2005, 2008, or 2008 R2
  • Microsoft Visual Studio 2010 Professional SP1
  • Portable Extensible Metadata (VS2010 Extension)
  • (Optional) Tangible T4 Editor plus modeling tools for VS2010 (VS2010 Extension)

To create a new project similar to SchoolSample.Data.Wcf, we need to first add an ADO.NET Entity Data Model item, and switch its "Code Generation Strategy" to "None".

Image 16

After that, add the three T4 template files from the SchoolSample.Data.Wcf project into this new project, and rename them to anything of your choice. For, set the "inputFile" value to where the new EDM file is located, and set "namespaceName" to a new namespace of your choice.

Image 17

For, set the "inputFile" and "namespaceName" values just as we did above.

Image 18

For, besides setting "inputFile" and "namespaceName" as we did above, we also need to set the "codeForSilverlight", "codeForWPF", and "codeForIEditableObject" values.

Image 19

After that, all we have to do is right-click on each of the three T4 template files and select "Run Custom Tool". This will generate all the self-tracking entity classes based on our new EDM file.

There is no special step to follow when creating a new project similar to the project SchoolSample.Data for Windows XP. Just follow the steps described in the section Project SchoolSample.Data above.

Issue With Entity Framework 4.0

There is one issue when running the demo under Microsoft .NET 4.0. To replicate the problem, we need to first add a few new students, instructors, and two new courses. Next, enroll students to the two new courses and save all changes. We should not see any problem for these two steps. After that, choose one student that already enrolled in the two new courses, and try to delete both enrollments and then save all changes. We will see a warning message similar to the one shown below.

Image 20

This, in fact, is a bug for Entity Framework 4.0 as documented here (the principal entity in a SQL application generates unnecessary updates when the application uses the Entity Framework in .NET Framework 4), and we can easily capture the problem with SQL Server Profiler as follows:

Image 21

As we are only deleting enrollment information, the updates to both Course and Person entities are all unnecessary. And, because the demo application uses Concurrency Mode=Fixed, the first delete/update would erroneously prevent the second delete/update from going through.

To resolve this problem in production systems, we need to contact Microsoft Support for the hot fix which is already available (but not free). For personal testing purposes, we could build a virtual machine with Microsoft Entity Framework June 2011 CTP, which also has this issue fixed. The screenshot below shows this demo application running with Entity Framework June 2011 CTP, and we can see that deleting an enrollment does not trigger the two updates to Course and Person entities. The Save All Changes goes through seamlessly.

Image 22

Wrapping up

In this article, we introduced the Visual Studio 2010 Extension "Self-Tracking Entity Generator for WPF/Silverlight" as well as all the necessary steps to set up an environment to build and run the SchoolSample demo application.

I hope you find this article useful, and please rate and/or leave feedback below. Thank you!


  • 07 December, 2011 - Initial release.
  • 19 January, 2012 - Updated source code.
  • 20 February, 2012 - Updated source code.


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

Written By
Software Developer (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

Comments and Discussions

QuestionCannot create course Pin
Claire Streb10-Jun-13 7:21
Claire Streb10-Jun-13 7:21 
AnswerRe: Cannot create course Pin
Weidong Shen10-Jun-13 9:06
Weidong Shen10-Jun-13 9:06 
GeneralRe: Cannot create course Pin
Claire Streb10-Jun-13 10:04
Claire Streb10-Jun-13 10:04 
QuestionCannot view xmal design view Pin
jim191026-Aug-12 9:50
jim191026-Aug-12 9:50 
AnswerRe: Cannot view xmal design view Pin
Weidong Shen26-Aug-12 15:33
Weidong Shen26-Aug-12 15:33 
QuestionVery interesting Pin
Ahmed.mb16-Feb-12 0:40
Ahmed.mb16-Feb-12 0:40 
AnswerRe: Very interesting Pin
Weidong Shen16-Feb-12 3:32
Weidong Shen16-Feb-12 3:32 
Questionis bug? ObjectGraphHasChanges Pin
Giovanni Caputo9-Feb-12 0:57
Giovanni Caputo9-Feb-12 0:57 
AnswerRe: is bug? ObjectGraphHasChanges Pin
Weidong Shen9-Feb-12 5:12
Weidong Shen9-Feb-12 5:12 
GeneralRe: is bug? ObjectGraphHasChanges Pin
Giovanni Caputo9-Feb-12 21:08
Giovanni Caputo9-Feb-12 21:08 
GeneralRe: is bug? ObjectGraphHasChanges Pin
Weidong Shen11-Feb-12 7:33
Weidong Shen11-Feb-12 7:33 
QuestionSource code is empty Pin
Giovanni Caputo7-Feb-12 0:49
Giovanni Caputo7-Feb-12 0:49 
AnswerRe: Source code is empty Pin
Weidong Shen7-Feb-12 4:58
Weidong Shen7-Feb-12 4:58 
AnswerRe: Source code is empty Pin
Weidong Shen7-Feb-12 15:39
Weidong Shen7-Feb-12 15:39 
QuestionThe KB fix Pin
Luka19-Jan-12 6:33
Luka19-Jan-12 6:33 
AnswerRe: The KB fix Pin
Weidong Shen19-Jan-12 7:05
Weidong Shen19-Jan-12 7:05 
GeneralMy vote of 5 Pin
Tony Bacon14-Dec-11 5:37
Tony Bacon14-Dec-11 5:37 
GeneralRe: My vote of 5 Pin
Weidong Shen14-Dec-11 6:35
Weidong Shen14-Dec-11 6:35 
GeneralMy vote of 5 Pin
nz_ham7-Dec-11 9:11
nz_ham7-Dec-11 9:11 
GeneralRe: My vote of 5 Pin
Weidong Shen7-Dec-11 11:09
Weidong Shen7-Dec-11 11:09 

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.