C# 从实体框架中的db列生成属性。在列allow NULL的情况下,为什么属性的setter中没有if条件

C# 从实体框架中的db列生成属性。在列allow NULL的情况下,为什么属性的setter中没有if条件,c#,entity-framework,c#-4.0,entity-framework-4,C#,Entity Framework,C# 4.0,Entity Framework 4,如果列allow不允许在db中使用NULL,则实体框架中生成的属性的setter中的条件是If(_ColumnName!=value) 这有什么用 为什么从允许NULL的列生成的setter中缺少此条件 [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] [DataMemberAttribute()] public global::System.String Address1 { get {

如果列allow不允许在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;

不,if条件与属性是否可为null无关。仅当属性为EntityKey时才会生成。

深入研究(这是默认的代码生成工具,也是VS2010用于生成实体对象的工具)可以发现:

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();
#>
            }
<#+
            }
#>
        }
if(ef.IsKey(primitiveProperty))
{
if(ef.ClrType(primitiveProperty.TypeUsage)==typeof(byte[]))
{
#>
如果(!StructuralObject.BinaryEquals(,value))
//在这里,它注入了if条件:
如果(!=值)
{
(价值);
报告财产变更(“”);
=StructuralObject.SetValidValue(值);
报告财产变更(“”);
();
}
}

Address1和Address2都不是EntityKey您的Address1属性用[EdmScalarPropertyAttribute(EntityKeyProperty=true…)属性注释,这意味着它是EntityKey。