C# 如何在EntityDataModel的分部类中修改对象的值

C# 如何在EntityDataModel的分部类中修改对象的值,c#,entity-framework,partial-methods,C#,Entity Framework,Partial Methods,我有一个从数据库生成的EntityDataModel(Visual Studio 2010,asp.net 4.0,c#)。 我试图使用与实体类关联的分部类来执行一些业务逻辑(在本例中,检查电话号码字段并删除空格) 如果我使用这样的东西: partial void OnMobilePhoneNoChanged() { if (MobilePhoneNo != null) { MobilePhoneNo = ATG_

我有一个从数据库生成的EntityDataModel(Visual Studio 2010,asp.net 4.0,c#)。 我试图使用与实体类关联的分部类来执行一些业务逻辑(在本例中,检查电话号码字段并删除空格)

如果我使用这样的东西:

 partial void OnMobilePhoneNoChanged()  
    {  
        if (MobilePhoneNo != null)  
        {  
            MobilePhoneNo = ATG_COModel_Common.FormatPhoneNumber(MobilePhoneNo);  
        }  
    }  
然后我得到了一个无限循环(因为我的FormatPhoneNumber方法修改了MobilePHoneNo,它再次引发事件等等),然后我得到。。。堆栈溢出

当我尝试改用onmobilephonechanging并修改MobilePHoneNo属性(或
值)时,该值没有正确保存


我该怎么办?

查看您模型的
.Designer.cs
文件。您将看到如下内容:

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.String MobilePhoneNo 
    {
        get
        {
            return _MobilePhoneNo;
        }
        set
        {
            OnMobilePhoneNoChanging(value);
            ReportPropertyChanging("MobilePhoneNo");
            _MobilePhoneNo = StructuralObject.SetValidValue(value, false);
            ReportPropertyChanged("MobilePhoneNo");
            OnMobilePhoneNoChanged();
        }
    }
    private global::System.String _MobilePhoneNo;
    partial void OnMobilePhoneNoChanging(global::System.String value);
    partial void OnMobilePhoneNoChanged();
这就是你想要的

partial void OnMobilePhoneNoChanged()  
{  
    if (_MobilePhoneNo != null)  
    {  
        _MobilePhoneNo = ATG_COModel_Common.FormatPhoneNumber(_MobilePhoneNo);  
    }  
}