SiberianGuy
SiberianGuy

Reputation: 25312

Entity Framework change tracking

Is there any way of tracking EF entities changes between contexts for ASP.NET applications?

Self-Tracking entities doesn't work well for me as it is primarily designed for WCF. And all approaches for tracking changes for POCO I have found are oriented on shared context.

Upvotes: 2

Views: 2038

Answers (2)

Mike Vonn
Mike Vonn

Reputation: 184

I suggest using the Change Tracking built into SQL Server (http://technet.microsoft.com/en-us/library/cc280462(v=sql.105).aspx). This framework allows you to identify whether a given row has changed, or even if a column has changed. You do need to somehow manage the question of changed since when. The Change Tracking in SQL server is accomplished by passing the table and desired 'since when' revision number into the ChangeTable function (http://technet.microsoft.com/en-us/library/bb934145.aspx). You can use the results of this function to determine when a table was last changed, and the primary key of the table. As far as I know you can only use this with tables that have a primary key defined. You can then create a table valued function or a view that returns the records that have changed. Both of these are easy to consume using the Entity Framework.

Upvotes: 0

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364389

No you have to track changes by yourselves or you have to use STE and store them in ViewState/Session between postbacks.

Edit: If you work with simple entity you can use some methods to track changes for you but first you have to load the entity from database (= additional database query). Then you can use for example ApplyCurrentValues method of the ObjectContext instance. This appraoch doesn't work for updating object complex graphs.

Upvotes: 2

Related Questions