c# - DbContext 和 ObjectContext 之间不同的 setter 行为

标签 c# entity-framework entity-framework-4 dbcontext objectcontext

(这是使用EntityFramework 4.2 CTP)

我还没有在网络上找到任何对此的引用,尽管我在搜索时可能使用了错误的术语。还有一种很可能的情况是,这是 100% 预期的行为,只是寻求确认,而不愿意深入研究 tt 模板(对此仍然很陌生)。

假设我有一个带有名为 Active 的 bool 字段的类,并且我有一行已将此值设置为 true。我有执行将所述字段设置为 True 的代码,无论其现有值如何。

  • 如果我使用 DbContext 将值更新为 True,则不会进行任何更新。
  • 如果我使用 ObjectContext 更新值,则会进行更新 无论现有值(value)如何。

这发生在完全相同的 EDMX 中,我所做的只是将代码生成模板从 DbContext 更改为 EntityObject。

更新:

好的,找到了我正在寻找的确认信息...认为这是一个骗局...下次我将进行 MOAR 搜索!

Entity Framework: Cancel a property change if no change in value

更新2:

答案(或者至少是我找到的解决方案)已根据要求转移到实际答案...

最佳答案

根据要求,回答我自己的问题(抱歉)...

问题:默认的 tt 模板将 setter 中的“if (this != value)”包装为“if (iskey)”,因此只有主键字段会接收此逻辑。

解决方案:这不是最优雅的事情,但我删除了这个检查......我们将看看它在实际使用中的效果如何。我包含了整个 tt 模板,我的更改用 ** 表示...

更新:在 Entity-Framework 5 Beta 2 中使用相同的方法进行确认和解决

////////
////////  Write SimpleType Properties.
////////
private void WriteSimpleTypeProperty(EdmProperty simpleProperty, CodeGenerationTools code)
{
    MetadataTools ef = new MetadataTools(this);
#>

/// <summary>
/// <#=SummaryComment(simpleProperty)#>
/// </summary><#=LongDescriptionCommentElement(simpleProperty, 1)#>
[EdmScalarPropertyAttribute(EntityKeyProperty=       <#=code.CreateLiteral(ef.IsKey(simpleProperty))#>, IsNullable=<#=code.CreateLiteral(ef.IsNullable(simpleProperty))#>)]
[DataMemberAttribute()]
<#=code.SpaceAfter(NewModifier(simpleProperty))#><#=Accessibility.ForProperty(simpleProperty)#> <#=MultiSchemaEscape(simpleProperty.TypeUsage, code)#> <#=code.Escape(simpleProperty)#>
{
    <#=code.SpaceAfter(Accessibility.ForGetter(simpleProperty))#>get
    {
<#+             if (ef.ClrType(simpleProperty.TypeUsage) == typeof(byte[]))
            {
#>
        return StructuralObject.GetValidValue(<#=code.FieldName(simpleProperty)#>);
<#+
            }
            else
            {
#>
        return <#=code.FieldName(simpleProperty)#>;
<#+
            }
#>
    }
    <#=code.SpaceAfter(Accessibility.ForSetter((simpleProperty)))#>set
    {
<#+
        **//if (ef.IsKey(simpleProperty)) 
        **//{
            if (ef.ClrType(simpleProperty.TypeUsage) == typeof(byte[]))
            {
#>
        if (!StructuralObject.BinaryEquals(<#=code.FieldName(simpleProperty)#>, value))
<#+
            }
            else
            {
 #>
        if (<#=code.FieldName(simpleProperty)#> != value)
<#+
            }
#>
        {
<#+
    PushIndent(CodeRegion.GetIndent(1));
        **//}
#>
        <#=ChangingMethodName(simpleProperty)#>(value);
        ReportPropertyChanging("<#=simpleProperty.Name#>");
        <#=code.FieldName(simpleProperty)#> = <#=CastToEnumType(simpleProperty.TypeUsage, code)#>StructuralObject.SetValidValue(<#=CastToUnderlyingType(simpleProperty.TypeUsage, code)#>value<#=OptionalNullableParameterForSetValidValue(simpleProperty, code)#>, "<#=simpleProperty.Name#>");
        ReportPropertyChanged("<#=simpleProperty.Name#>");
        <#=ChangedMethodName(simpleProperty)#>();
<#+
    **//if (ef.IsKey(simpleProperty))
        **//{
    PopIndent();
#>
        }
<#+
        **//}
#>
    }
}

关于c# - DbContext 和 ObjectContext 之间不同的 setter 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9876054/

相关文章:

用于解析人类可读时间跨度的 C# 库

c# - 添加一个 web.config 键,以便在遇到未处理的异常时始终重定向

c# - 通过 Linq 查询使用子集合中的条件过滤父集合

entity-framework-4 - 将 EF4 DbContext 与域服务一起使用

c# - 在 .Net 中获取子字符串时,新字符串是引用相同的原始字符串数据还是复制数据?

c# - Rhinomock 期望中的非原始对象

c# - EF6 迁移失败

c# - 将实体上下文传递给其他方法和对象

c# - Entity Framework 按查询中的行字段降序排序

linq - 使用 Linq to Entities (EF4.0) 输入和不输入