Entity Framework 4 - How to inject logic in property setter?

▼魔方 西西 提交于 2019-12-24 07:16:08

问题


I have a property auto-generated from database in my edmx: Description. I then create a "partial class" .cs file for the entity and add a read-only property: ShortDescription. ShortDescription's getter simply processes Description (removes line feed, carriage return, etc).

How can I raise property change notification for ShortDescription on the setter of Description?

Thanks!


回答1:


This is going to be a hack, but it can be done.

First, you need to override ReportPropertyChanging and ReportPropertyChanged. Then check the parameter for the name of your property... in this case "Description". If it occurs, call ReportPropertyChanging or ReportPropertyChanged with the derived property name, in this case "ShortDescription". For any other value of the parameter, call the base version of ReportPropertyChanging/Changed.

Edit: For example:

    protected override void OnPropertyChanging(string property)
    {
        if (property == "Description")
        {
            base.OnPropertyChanging("ShortDescription");
        }
        base.OnPropertyChanging(property);
    }

    protected override void OnPropertyChanged(string property)
    {
        if (property == "Description")
        {
            base.OnPropertyChanged("ShortDescription");
        }
        base.OnPropertyChanged(property);
    }



回答2:


The methods are partial also, so in your partial class you can add code like this

 partial void OnDescriptionChanged()
  {
    OnPropertyChanged("ShortDescription"); 
  }


来源:https://stackoverflow.com/questions/3729046/entity-framework-4-how-to-inject-logic-in-property-setter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!