【发布时间】:2011-09-08 14:21:23
【问题描述】:
从 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();
我希望所有属性都有这种行为。 我是否缺少模型设计器中的设置,还是有其他解决方案?
谢谢!
【问题讨论】:
-
而且我知道我可以为此自定义 T4 模板...但我宁愿不在我的项目中使用自定义模板!
标签: entity-framework-4 inotifypropertychanged