1

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);
    }
}
Ucodia
  • 7,410
  • 11
  • 47
  • 81
  • Possible duplicate of http://stackoverflow.com/questions/3595404/how-not-persist-property-ef4-code-first , try using [NotMapped] – undefined Mar 06 '12 at 10:33
  • I think NotMapped only works with Code First. I am using Model First throught the EDM designer here. – Ucodia Mar 06 '12 at 11:21
  • Im pretty sure given you are modifying the T4 and have access to the generated entities it should work (obviously EF4.1+), you could give it a go, otherwise you ignore stuff by deleting it from the model view I think. – undefined Mar 06 '12 at 20:11
  • This should work without any problem. How exactly did you add the property to your T4 template. Did you mark it with any attribute? – Ladislav Mrnka Mar 06 '12 at 20:53
  • @Luke: Yes, it is what I am doing, just simply adding a backing field and a property accessor with an internal set in code generation. – Ucodia Mar 07 '12 at 09:14
  • @Ladislav: I also thought it should not make any problem, but even thought this property does not exist in the conceptual model but only in generated code, the framework sends me this error whenever the entity is attached to the context. And no I did not applied any attribute. – Ucodia Mar 07 '12 at 09:15

1 Answers1

1

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.

Ucodia
  • 7,410
  • 11
  • 47
  • 81