【问题标题】:Different setter behavior between DbContext and ObjectContextDbContext 和 ObjectContext 之间的不同 setter 行为
【发布时间】:2012-04-10 04:31:23
【问题描述】:

(这是使用 EntityFramework 4.2 CTP)

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

假设我有一个带有名为 Active 的布尔字段的类,并且我有一行已经将此值设置为 true。我有执行将所述字段设置为 True 的代码,而不管它的现有值如何。

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

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

更新:

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

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

更新 2:

答案(或至少我找到的解决方案)已按要求移至实际答案...

【问题讨论】:

  • 请将您的解决方案作为答案。
  • 一旦我的 8 小时“自我回答”等待结束,我会在需要时发布。

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


【解决方案1】:

根据要求,我自己的问题的答案(对不起)...

问题:默认的tt模板将setter中的“if(this != value)”用“if(iskey)”包裹起来,所以只有primarykey字段接收到这个逻辑。

解决方案:这不是最优雅的事情,但我删除了这个检查......我们将看看它在实际使用中的效果。我包含了整个 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();
#>
        }
<#+
        **//}
#>
    }
}

【讨论】:

    猜你喜欢
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2014-11-01
    相关资源
    最近更新 更多