实体框架 4:PropertyChanged 事件引发过于频繁

发布于 2024-12-04 03:34:36 字数 1976 浏览 6 评论 0原文

从 EF 为实体的属性生成的代码如下所示:

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.DateTime DateCreated
        {
            get
            {
                return _DateCreated;
            }
            set
            {
                OnDateCreatedChanging(value);
                ReportPropertyChanging("DateCreated");
                _DateCreated = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("DateCreated");
                OnDateCreatedChanged();
            }
        }
        private global::System.DateTime _DateCreated;
        partial void OnDateCreatedChanging(global::System.DateTime value);
        partial void OnDateCreatedChanged();

此代码不会检查值是否实际已更改(在 setter 中)。因此,即使您设置的值等于当前值,也会引发 PropertyChanged 事件。但在这种情况下,什么都不会改变,所以我不想要这个事件...

对于 EntityKey 属性,他们会检查这一点:

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Guid Id
        {
            get
            {
                return _Id;
            }
            set
            {
                if (_Id != value)
                {
                    OnIdChanging(value);
                    ReportPropertyChanging("Id");
                    _Id = StructuralObject.SetValidValue(value);
                    ReportPropertyChanged("Id");
                    OnIdChanged();
                }
            }
        }
        private global::System.Guid _Id;
        partial void OnIdChanging(global::System.Guid value);
        partial void OnIdChanged();

我希望所有属性都有这种行为。 我是否缺少模型设计器中的设置,或者还有其他解决方案吗?

谢谢!

the generated code from EF for a property of an entity looks like this:

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.DateTime DateCreated
        {
            get
            {
                return _DateCreated;
            }
            set
            {
                OnDateCreatedChanging(value);
                ReportPropertyChanging("DateCreated");
                _DateCreated = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("DateCreated");
                OnDateCreatedChanged();
            }
        }
        private global::System.DateTime _DateCreated;
        partial void OnDateCreatedChanging(global::System.DateTime value);
        partial void OnDateCreatedChanged();

This code doesn't check if the value has actually changed (in the setter). Therefore the PropertyChanged event is raised even if you set a value that is equal to the current value. But in this case nothing would have changed, so I wouldn't want this event...

For EntityKey properties they do check this:

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Guid Id
        {
            get
            {
                return _Id;
            }
            set
            {
                if (_Id != value)
                {
                    OnIdChanging(value);
                    ReportPropertyChanging("Id");
                    _Id = StructuralObject.SetValidValue(value);
                    ReportPropertyChanged("Id");
                    OnIdChanged();
                }
            }
        }
        private global::System.Guid _Id;
        partial void OnIdChanging(global::System.Guid value);
        partial void OnIdChanged();

I would expect this behavior from all properties.
Am I missing a setting in the model designer, or is there another solution?

Thanx!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有一腔孤勇 2024-12-11 03:34:36

T4 模板的要点是允许您进行所需的修改。这种说法绝对是错误的:

但我不想在我的项目中使用自定义模板!

这就像抛弃 T4 模板的所有优点并返回到硬编码的自定义工具来生成代码。

It is point of T4 templates to allow you modifications you need. It is absolutely wrong approach to say:

But I would rather not use a custom template in my project!

It is like throwing all advantages of T4 templates away and going back to hardcoded custom tools for code generating.

打小就很酷 2024-12-11 03:34:36

正如我知道这是可能的(Ladislav 也指出的),我确实将 T4 模板文件包含到项目中,并对“Write PrimitiveType Properties”进行了以下更改。模板的一部分:

            if (!Object.Equals(<#=code.FieldName(primitiveProperty)#>, value))
            {
                <#=ChangingMethodName(primitiveProperty)#>(value);
                ReportPropertyChanging("<#=primitiveProperty.Name#>");
                <#=code.FieldName(primitiveProperty)#> = StructuralObject.SetValidValue(value<#=OptionalNullableParameterForSetValidValue(primitiveProperty, code)#>);
                ReportPropertyChanged("<#=primitiveProperty.Name#>");
                <#=ChangedMethodName(primitiveProperty)#>();
            }

希望对其他人有帮助。

I did, as I knew it was possible and Ladislav also stated, include the T4 template file into the project and made the following changes to the "Write PrimitiveType Properties." part of the template:

            if (!Object.Equals(<#=code.FieldName(primitiveProperty)#>, value))
            {
                <#=ChangingMethodName(primitiveProperty)#>(value);
                ReportPropertyChanging("<#=primitiveProperty.Name#>");
                <#=code.FieldName(primitiveProperty)#> = StructuralObject.SetValidValue(value<#=OptionalNullableParameterForSetValidValue(primitiveProperty, code)#>);
                ReportPropertyChanged("<#=primitiveProperty.Name#>");
                <#=ChangedMethodName(primitiveProperty)#>();
            }

Hope that will be helpfull to others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文