Contents
Introduction
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)
Installation
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.
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.
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.
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:
<connectionStrings>
<add name="SchoolEntities" connectionString="metadata=res://
*/EntityModel.SchoolModel.csdl|res://*/EntityModel.SchoolModel.ssdl|res://
*/EntityModel.SchoolModel.msl;provider=System.Data.SqlClient;
provider connection string="data source=localhost;initial catalog=SCHOOL;
integrated security=True;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
When the demo compiles and runs successfully, we should be able to see this WPF application running like in the screenshot below:
Architecture
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:
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.
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...".
Next, we will be prompted with the "Add New Item" dialog box. Select "Self-Tracking Entity Generator for WPF/Silverlight", type "SchoolDataModel.tt" in the Name field, and hit the Add button.
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.
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.
The first thing to do for this project is to set a conditional compilation symbol WPF for all configurations, as shown below:
The folder EntityModel hosts links to the files SchoolDataModel.Client.tt and SchoolDataModel.tt 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:
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.
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".
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 SchoolDataModel.tt, set the "inputFile
" value to where the new EDM file is located, and set "namespaceName
" to a new namespace of your choice.
For SchoolDataModel.Context.tt, set the "inputFile
" and "namespaceName
" values just as we did above.
For SchoolDataModel.Client.tt, besides setting "inputFile
" and "namespaceName
" as we did above, we also need to set the "codeForSilverlight
", "codeForWPF
", and "codeForIEditableObject
" values.
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.
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:
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.
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!
History
- 07 December, 2011 - Initial release.
- 19 January, 2012 - Updated source code.
- 20 February, 2012 - Updated source code.