Print Page | Close Window

Detecting changes to a new Entity

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1663
Printed Date: 21-Sep-2025 at 2:12am


Topic: Detecting changes to a new Entity
Posted By: chuckc
Subject: Detecting changes to a new Entity
Date Posted: 27-Feb-2010 at 4:39pm
Hopefully this is a simple question to answer.

When creating a new object/Entity, how does one skip attempting to save it if it has not been modified? I'm trying to add some logic into a Saving event handler, but EnityAspect.HasChanges() seems to always return true as soon as an object has been created, even before any properties have been modified.   AcceptChanges() and RejectChanges() seem to have no effect.

Employee e = _manager.CreateEntity<Employee>();
// e.EntityAspect.HasChanges() == true

e.EntityAspect.AcceptChanges();
// e.EntityAspect.HasChanges() == true

e.EntityAspect.RejectChanges();
// e.EntityAspect.HasChanges() == true

The above code behaves the same even when the Employee is created with a simple constructor, e.g. Employee e = new Employee();

Is there some other technique that you recommend?

Thanks.



Replies:
Posted By: davidklitzke
Date Posted: 04-Mar-2010 at 10:43am

EntityAspect.HasChanges() is currently working as designed.

If you want different semantics, you will have to handle it yourself.  For example, if you want to know if an entity has changed since it was initially created, you could add an additional property to the entity such as ChangedAfterCreated.  You would first set the value of this property to false when you created the entity.  You could then add a PropertyChanged event handler to handle the ChangedAfterCreated property.


Posted By: chuckc
Date Posted: 04-Mar-2010 at 12:23pm
Thanks for the reply, David.

So, it sounds like the EntityAspect property of a DevForce entity just returns the underlying EntityFramework entity.

That seems to imply that the upcoming April version of DevForce that supports Entity Frameworks 4.0 will have the improved self-tracking features that are supposed to be coming in EF4, as discussed here:

http://www.develop.com/entityframework4

Can I get a beta copy of DevForce for EF4?



Print Page | Close Window