简体   繁体   English

从Entity Framework中的db列生成的属性。 为什么在列的情况下属性设置器中不存在if条件,则允许NULL

[英]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 ) 如果column allow不允许db中的NULL,则Entity Framework中生成的属性的setter中的条件为if (_ColumnName != value

What is the use of this? 有什么用?

Why this condition is missing in setter generated from columns which allow NULL? 为什么从允许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;

No, that if condition has nothing to do with whether or not the property is Nullable. 不,如果条件与该属性是否为Nullable无关。 It will be generated only when the property is EntityKey . 仅当属性为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: 深入研究ADO.NET EntityObject Generator T4模板 (这是默认的代码生成工具,它是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();
#>
            }
<#+
            }
#>
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在模型中声明一个属性,该属性不会因Entity Framework迁移而在数据库中作为列生成 - Declaring a property in a model that will not get generated as a column in the database by Entity Framework migrations 在CodeFirst实体框架中为模型中的db中的现有列添加属性 - Adding Property to Model in CodeFirst entity framework for an existing column in db 添加不在数据库表中的属性时,实体框架无效的列名 - Entity Framework Invalid column name when adding property that is not in db table 实体框架-ICollection属性始终为空-getter setter有麻烦吗? - Entity Framework - ICollection Property Always Null - getter setter troubles? 实体框架。加载子类属性的Quering父实体 - Entity framework. Quering parent entity with subclass property loaded 实体框架列名称到属性映射数据库 - entity framework column name to property mapping dbentry 实体框架将集合属性映射到一列 - Entity Framework map collection property to one column 实体框架生成意外的属性 - Unexpected property generated by Entity Framework 实体框架-属性设置器中的自定义逻辑 - Entity Framework - Custom logic in property setter 实体框架 6 如何将 UserDefined 数据类型属性映射为 DB 中的 varchar 类型列 - Entity Framework 6 How to Map a UserDefined datatype property as varchar type column in DB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM