Error encountered on BeginEdit() method

Sep 21, 2013 at 11:37 AM
Edited Sep 21, 2013 at 11: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; }
            set
            {
                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:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' 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.

Thanks!
Coordinator
Sep 22, 2013 at 4: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.

Cheers,
Tony