views:

16

answers:

1

All entity created by EF is partial class. so it is extendable. Suppose I have entity Person like

partial class Person{FirstName, LastName, .....}

Then I want to add a compute property Name like:

partial class Person{

[DataMember]        
public string Name
{
   get { return String.Format("{0} {1}", this.FirstName, this.LastName); }
}

partial void OnFirstNameChanged()
{
  //.....
  this.ReportPropertyChanged("Name");
}

partial void OnLastNameChanged()
{
  //.....
  this.ReportPropertyChanged("Name");
}
//....
}

Then for data upate operation I got following error: The property 'Name' does not have a valid entity mapping on the entity object. For more information, see the Entity Framework documentation.

How to fix this solution?

A: 

The problem is with those ReportPropertyChanged("Name"), you are reporting to ObjectStateManager that the "Name" property has been changed, while this property does not exists in your model metadata (it has just been declared in your partial class, ObjectContext and ObjectStateManager do not know anything about this property).
If you add those OnLastNameChanged and OnFirstNameChanged partial methods, just get rid of them, you don't need them.

Morteza Manavi