I have a EntityDataModel generated from my database (Visual Studio 2010, asp.net 4.0, c#). I am trying to use a partial class associated to an entity class to perform some business logic (in this case check a phone number field and remove spaces).
If I use something like that:
partial void OnMobilePhoneNoChanged()
{
if (MobilePhoneNo != null)
{
MobilePhoneNo = ATG_COModel_Common.FormatPhoneNumber(MobilePhoneNo);
}
}
then I end up getting an infinite loop (because my FormatPhoneNumber method modifies MobilePHoneNo which raises the event again etc) and then I get... a stack overflow!
When I try using the OnMobilePhoneNoChanging instead, and modify the MobilePHoneNo property (or the value value) then the value is not saved properly.
What should I do ?
Have a look in your model's
.Designer.csfile. You'll see something like this:Notice that as well as the partial
ChangingandChangedmethods you already know about, there's a backing field. Because your code is in a partial piece of the class, you have access to all members, including the private ones. So you can implement the partialChangedmethod, and directly alter_MobilePhoneNo:which is what you want.