如果列允许在db中不允许NULL,则实体框架中生成的属性的setter中的条件为if (_ColumnName != value
)
这有什么用?
为什么从允许NULL的列生成的setter中缺少这种情况?
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String Address1
{
get
{
return _Address1;
}
set
{
if (_Address1 != value) // This condition is only for columns which are not null in db. Why this is not needed for nullable columns.
{
OnAddress1Changing(value);
ReportPropertyChanging("Address1");
_Address1 = StructuralObject.SetValidValue(value, false);
ReportPropertyChanged("Address1");
OnAddress1Changed();
}
}
}
private global::System.String _Address1;
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String Address2
{
get
{
return _Address2;
}
set
{
// NO IF CONDITION HERE LIKE IT IS IN PROPERTY WHICH DON'T ALLOW NULL (ABOVE PROPERTY ADDRESS1)
OnAddress2Changing(value);
ReportPropertyChanging("Address2");
_Address2 = StructuralObject.SetValidValue(value, true);
ReportPropertyChanged("Address2");
OnAddress2Changed();
}
}
private global::System.String _Address2;
答案 0 :(得分:2)
不,如果条件与属性是否为Nullable无关。仅当属性为 EntityKey 时才会生成它。
深入研究 ADO.NET EntityObject Generator T4 templte (这是默认的代码生成工具,是VS 2010用来生成实体对象的工具)揭示了这一点:
if (ef.IsKey(primitiveProperty))
{
if (ef.ClrType(primitiveProperty.TypeUsage) == typeof(byte[]))
{
#>
if (!StructuralObject.BinaryEquals(<#=code.FieldName(primitiveProperty)#>, value))
<#+
}
else
{
#>
// Here it inject the if condition:
if (<#=code.FieldName(primitiveProperty)#> != value )
<#+
}
#>
{
<#+
PushIndent(CodeRegion.GetIndent(1));
}
#>
<#=ChangingMethodName(primitiveProperty)#>(value);
ReportPropertyChanging("<#=primitiveProperty.Name#>");
<#=code.FieldName(primitiveProperty)#> = StructuralObject.SetValidValue(value<#=OptionalNullableParameterForSetValidValue(primitiveProperty, code)#>);
ReportPropertyChanged("<#=primitiveProperty.Name#>");
<#=ChangedMethodName(primitiveProperty)#>();
<#+
if (ef.IsKey(primitiveProperty))
{
PopIndent();
#>
}
<#+
}
#>
}