Serializing Self-Tracking Entities
In an ADO.NET Entity Framework application, an object context is responsible for tracking changes in your objects. However, when you need to manipulate objects in another tier, where the object context is not available, you must decide how to track changes report those changes back to the object context. Starting with the Entity Framework version 4, self-tracking entities can help you track changes in any tier. A self-tracking entity is an entity built from a Text Template Transformation Toolkit (T4) template that generates entity types that have the ability to record changes to scalar, complex, and navigation properties. The ADO.NET Self-Tracking Entity Generator Template is available in Visual Studio 2010. For more information, see Walkthrough: Serialize Self-Tracking Entities.
This sample is available on the Entity Framework Documentation Samples site on MSDN Code Gallery.
The sample demonstrates how a WCF service exposes a series of operations that return entity graphs. Next, a client application manipulates that graph and submits the modifications to a service operation that validates and saves the updates to a database by using the Entity Framework.
Before running this sample, make sure the following software is installed:
Visual Studio 2010 containing the .NET Framework 4
SQL Server 2005 or 2008 Database Engine
School sample database (included in the sample)
SQL Server Management Studio (optional)
Building the Sample
The STESchoolModelExample consists of 5 projects:
STESchoolModel class library project that contains the School-based model and the self-tracking entity templates, which includes the self-tracking entity types template and the typed System.Data.Objects.ObjectContext template.
STESchoolModelTypes class library project that links to the self-tracking entity types template that is located in the STESchoolModel project.
STESchoolModelService Windows Communication Foundation (WCF) service application that exposes a set of operations that return entity graphs and apply changes made on the client to the database.
STESchoolModelConsoleTest console client applications.
STESchoolModelWPFTest The Windows Presentation Foundation (WPF) client application.
Use the following procedure to build the sample.
To build the STESchoolModelExample application
(Optional) If the School sample database is not already present in your SQL Server instance, run the CreateSchool.sql script included with the project.
Open the solution file in Visual Studio.
(Optional) If you created the School database in an instance of SQL Server other than the default instance on the local machine, specify the correct SQL Server instance by modifying the Data Source in the connection string in the configuration file of the following projects: STESchoolModelService, STESchoolModelConsoleTest, STESchoolModelWPFTest.
Build the solution.
Running the Sample
Use the following procedure to run the sample.
To run the client application
Update service reference in the STESchoolModelWPFTest and STESchoolModelConsoleTest client projects. To update the reference, in Solution Explorer, right-click ServiceReference1 under the Service References folder, and select Update Service Reference.
Set the STESchoolModelWPFTest or STESchoolModelConsoleTest as the start-up project.
Run the application. If the client failed to communicate with the referenced service, follow the next step.
Delete ServiceReference1 from the client project.
Add the service reference again. To add a reference to the service, in Solution Explorer, right-click the Service References folder, and then select Add Service Reference.
Configure the service. Because STESchoolModelService returns System.Collections.Generic.List, you need to configure the service reference. Right-click the service name (ServiceReference1), and then select Configure Service Reference…. In the Configure Service Reference dialog, select the System.Collections.Generic.List type from the Collection type list.
Run the application.
Removing the Sample
Use the following procedure to remove the STESchoolModelExample sample.
To remove the STESchoolModelExample application
Delete the project directory and contents.
(Optional) Drop the School database from the instance of SQL Server.
ADO.NET Entity Framework
Entity Data Model
ADO.NET Entity Data Model Tools
Binding Objects to Controls
Walkthrough: Serialize Self-Tracking Entities