views:

425

answers:

4

Does the entity framework 4.0 track changes on Plain old C# object?

If so, should my class do something special (implement an interface/raise events when changing the value of a property?)

Also, how can I get the state(modified/unchanged, etc) of a POCO? Is there a way to know the "old" values of properties?

+2  A: 

I found POCO in the Entity Framework: Part 1 - The Experience by quick googling. See Part 3 – Change Tracking with POCO.

There are two types of change tracking possibilities with POCO: Snapshot based Change Tracking and Notification based Change Tracking with Proxies. In this post, I would like to drill into both options a bit further and cover the advantages and disadvantages of both, along with the implications of using either approach.

eed3si9n
A: 

I do not know how you are using POCO's with EF (no code posted with question), however if you us the Entities created by the EF you can track changes made. This MSDN article explains it. You can also read this blog post for even more details. If those don't help you, can you update your question with sample code that shows how you are using EF and how you are using POCO's with it. Trying to show an example of what you would like to see done with your POCO's with a mock up code example.

Rodney Foley
+1  A: 

The question has been changed to ask about the upcoming .net 4.0 support for POCOs. The answer below is in regard to the existing .net 3.5 support, as was originally asked. For information on .net 4.0 EF POCO support see other answers.


No. The current version of the entity framework does not support POCOs.

In order to be consumable by EF, entity classes have to:

  • Derive from EntityObject class or implement at least one mandatory IPOCO interface: IEntityWithChangeTracker. If the entity participates in relationships it also has to implement IEntityWithRelationships interface. Implementing those interfaces is not hard, but causes entity objects to have a dependency on EF assembly, which is sometimes not desirable

  • Provide assembly, class and property-level attributes to define mapping from CLR space to model space (we call those O-C mapping attributes)

  • Use Entity-Framework-provided relationship classes: EntityReference, EntityCollection and RelationshipManager instead of CLR collections (List, ICollection)

source

By far the easiest way to create classes that meet these requirements is to use the EF designer in visual studio to create an EDMX file. Part of this file contains the CSDL which defines the conceptual model of your data, in other words - the objects. The EDMX file also defines the SSDL (the storage/database) and the MSL (The mapping between SSDL and CSDL). It is this CSDL that will be used to generate the classes that will work with the EF.

POCO support is coming though in .net 4.0.

Simon P Stevens
The question is about EF 4.0, not the current version...
Thomas Levesque
Haha. It wasn't originally. Dam cheeky that is, changing the question after I've answered it.
Simon P Stevens
+1  A: 

If you use the dynamically generated proxies (which is the default behavior for POCOs if your classes meet the requirements), the changes on your entities will be tracked.

See this article for details : Tracking Changes in Persistence-Ignorant Objects

Thomas Levesque