Partager via


Updated Feature CTP Walkthrough: Self Tracking Entities for the Entity Framework


The information in this post is out of date.

Visit msdn.com/data/ef for the latest information on current and past releases of EF.


The goal of this walkthrough is to demonstrate a basic scenario for the use of the Self-Tracking Entities feature of Entity Framework.

 Self-Tracking Entities consist on a set of code generation templates based on Text Template Transformation Toolkit (T4) technology that is used to generate entity types that have the ability to record changes on scalar and complex property values, and on reference and collection navigation properties, independently of the Entity Framework.

The main scenario we are trying to address with Self-Tracking Entities is one in which a WCF service exposes a series of operations that return entity graphs, then a client application can manipulate that graph and submit the modifications to another service operation that validates changes in the graph and then applies them to the database store using Entity Framework.

While the following walkthrough uses C#, versions of the Self-Tracking Entities template exist for both C# and Visual Basic, and all the concepts described apply as well to the Visual Basic version.

Requirements

1. This walkthrough requires Visual Studio 2010 Beta 2.

2. The Microsoft Entity Framework Feature CTP 2 that is described here.

3. A local SQL Server 2008 Express instance has to be installed as SQLEXPRESS. Otherwise, changes to the connection string in the web.config file or regenerating the database from the included SQL scripts may be necessary.

4. Download and extract the initial solution attached to this post.

Read More...

 

EFFeatureCTP2_Walkthrough_STE.ZIP

Comments

  • Anonymous
    November 17, 2009
    Hi, I've been playing around with EF4 and the feature ctp and I noticed that context.SaveChanges() doesn't reset the ObjectState of the entities. From my feeling they all should be marked unchanged after I call SaveChanges. Am I wrong?

  • Anonymous
    November 20, 2009
    Where can I find any discussion which might be going on regarding the future of self-tracking entities and ASP.NET web forms?   Currently, to update a small number of fields for an entity using a web form I choose to re-query my BL, allow ASP.NET to update the bound fields, pass the updated entity to my BL, and apply changes (including FKs).  Before I get carried away I would really like to know when I might expect to be better off using self-tracking entities. (MVC is not in my future.) Also, being my first ever contact with the team I wanted to thank Diego, Danny, Alex and the whole Entity Framework team for what you have done. You had my vote of confidence the day I read about EF, when I understood the big picture which some people were too close (to their own product) to see. Keep up the pace of development and you'll continue to earn the respect you deserve. Graham

  • Anonymous
    December 03, 2009
    Hi, this is great, I like where EF for .Net4 is going. I noticed RecordOriginalValue is marked as internal (rightly so), but the OriginalValues is public so a client could potentially modify the change tracking history.

  • Anonymous
    December 07, 2009
    I just noticed that Self-Tracking entities do not support Binary Serialization which pretty much makes this not support ASP.Net applications that want to store in Session or ViewState. Are there any plans to support this?  This could be a big deal breaker for apps that want to do ASP.Net/WCF/Silverlight that share the same code base....

  • Anonymous
    December 07, 2009
    In my previous comment I said that I noticed Binary Serialization was not supported.  Just an FYI, I was able to get it working.  I haven't tested it, but it seems the problem was with the inherited Dictionary collections which needed to implement ISerializable the serialization constructor.  Once I did that and marked all classes with the SerializableAttribute that needed it, it worked fine.  I was able to serialize and deserialize (binary) just fine.  All the changetracker properties seem to have transfered fine.  (Am I missing something???  This seemed to be too easy to not be the case) Here is the code for one of the Dictionaries.... <Serializable()> _ <CollectionDataContract(Name:="ObjectsAddedToCollectionProperties", ItemName:="AddedObjectsForProperty", KeyName:="CollectionPropertyName", ValueName:="AddedObjects")> _ Public Class ObjectsAddedToCollectionProperties    Inherits Dictionary(Of String, ObjectList)    Implements ISerializable    Public Sub New()        MyBase.New()    End Sub    Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)        MyBase.New(info, context)    End Sub End Class