开发者

Generated property from db column in Entity Framework. Why an if condition is not there in setter of property in case of column allow NULL

If column allow does not allow NULL in db then a condition in setter of开发者_运维百科 generated property in Entity Framework is if (_ColumnName != value)

What is the use of this?

Why this condition is missing in setter generated from columns which allow NULL?

[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;


No, that if condition has nothing to do with whether or not the property is Nullable. It will be generated only when the property is EntityKey.

Digging into the ADO.NET EntityObject Generator T4 templte (which is the default code generation tool and is the one that VS 2010 uses to generate Entity Objects) reveals this:

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();
#>
            }
<#+
            }
#>
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜