Reputation: 7710
I am currently developping an application which object model and persistence layer is built using the Entity Framework model designer.
One of the requirements I have is to provide a status to my entities that could let me know whenever they are in a "dirty" state (modified), when the status change occurs and, most important, working in a disconnected mode. So I started modifying the T4 template to add a IsDirty
boolean property upon generation of those entities, added an event that is fired whenever IsDirty
changes and added this.IsDirty = true
in the xxxChanged methods of all scalar properties.
Everything works great when the entity is not attached to its context but when attached, whenever a property is changed, thus changing the IsDirty
value to false
, I receive this exception:
The property 'IsDirty' does not have a valid entity mapping on the entity object. For more information, see the Entity Framework documentation.
So what am I doing wrong here? I don't want this property to be mapped in my database as it is just an object status that only matters when the object is "alive". Is there an attribute I should use to decorate the IsDirty
property? Or should I derive EntityObject and implement the status mechanism? Or maybe you have any better advice on how to implement this?
Thanks.
Edit: I am using Entity Framework 4.0 with EDM designer.
Here is the piece of code generated into every base entity:
private bool isDirty;
public event EventHandler DirtyStatusChanged;
public bool IsDirty
{
get
{
return this.isDirty;
}
internal set
{
if (this.isDirty != value)
{
ReportPropertyChanging("IsDirty");
this.isDirty = value;
ReportPropertyChanged("IsDirty");
ReportDirtyStatusChanged();
}
}
}
protected void ReportDirtyStatusChanged()
{
var handler = this.DirtyStatusChanged;
if(handler != null)
{
handler(this, EventArgs.Empty);
}
}
Upvotes: 1
Views: 692
Reputation: 7710
I finally found the error myself. The problem lied inside my property setter. Instead of calling ReportPropertyChanged/ing
I should have called OnPropertyChanged/ing
.
ReportPropertyChanged/ing
implies for the context to search for changes between the original and the current entity value but as this property is just a status object that has no mapping to a store, the concept of original value makes no sense, thus making the context failing to find a correct mapping for this property.
Using OnPropertyChanged/ing
just fixed it.
Upvotes: 1