实体框架4-如何在属性设置器中注入逻辑? [英] Entity Framework 4 - How to inject logic in property setter?

查看:84
本文介绍了实体框架4-如何在属性设置器中注入逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在edmx中有一个从数据库自动生成的属性: 说明 .然后,我为实体创建一个部分类" .cs文件,并添加一个只读属性: ShortDescription . ShortDescription的getter只需处理Description(删除换行,回车等).

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).

如何在Description的设置者中引发ShortDescription的属性更改通知?

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

谢谢!

推荐答案

这将是一个hack,但可以做到.

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

首先,您需要覆盖 ReportPropertyChanging ReportPropertyChanged .然后检查参数以获取您的媒体资源的名称...在这种情况下为描述".如果发生这种情况,请使用派生的属性名称(在本例中为"ShortDescription")调用ReportPropertyChangingReportPropertyChanged.对于该参数的任何其他值,请调用ReportPropertyChanging/Changed的基本版本.

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.

例如:

    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);
    }

这篇关于实体框架4-如何在属性设置器中注入逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆