Error encountered on BeginEdit() method

Sep 21, 2013 at 10:37 AM
Edited Sep 21, 2013 at 10:39 AM
Hi folks,

I'm new to using Simple MVVM Toolkit and recently I was exploring the use of validation through IsValid and BeginEdit() method.

I am using Entity Framework 4.0 and I noticed that exception was consistently appearing upon calling the BeginEdit() method when one property of a class is marked virtual. When I remove virtual, the error goes away.
public virtual Location Location
            get { return location; }
                location = value;
                NotifyPropertyChanged(m => this.Location);
Here is the exception that was generated:
Type 'System.Data.Entity.DynamicProxies.ProductDetail_Locati_AB8A572B5ABF0A1B6B457C36EBB9BD3D358181709EF5C4F7D1763CFC5AFD9634' with data contract name 'ProductDetail_Locati_AB8A572B5ABF0A1B6B457C36EBB9BD3D358181709EF5C4F7D1763CFC5AFD9634:' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
Is there a way to resolve this error without removing the virtual keyword?

Any help would be appreciated.

Sep 22, 2013 at 3:33 PM
The short answer is that when you add a virtual property to an EF entity it is subclassed using a dynamic proxy, in order to provide features like lazy loading which are not, in my opinion, necessary most of the time. A side effect of the subclassing is that the entities are no longer serializable, which is required for cloning the object while editing it. My implementation of IEditable object takes a snapshot, or deep copy, of the object in order to undo changes while editing, in case the user cancels the edit. Avoid virtual properties if you want editing capability with EF entities.