可进行修改或QUOT

可进行修改或QUOT

本文介绍了实体框架5 - 为什么实体状态"可进行修改或QUOT;后为PropertyValue重新设置为原始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的是EF5,不知道为什么一个实体有国家改装后,我设置不仅改变了这个的PropertyValue实体回原来的值。

 使用(TestDbContext上下文=新TestDbContext())
{
字符串名称= context.Person。名字;

//计数为0
诠释计数= context.ChangeTracker.Entries()计数(E => e.State == EntityState.Modified)。

//更改值
context.Person.First()名称=测试。

//数为1
计数= context.ChangeTracker.Entries()计数。(E => e.State == EntityState.Modified);

//还原值
context.Person.First()名称=名称。


context.ChangeTracker.DetectChanges();

//数为1
计数= context.ChangeTracker.Entries()计数。(E => e.State == EntityState.Modified);
}



为什么呢? (


解决方案

由于实体框架只跟踪如果数据得到修正,而不是如果它是从它的原始内容不同

我们用一记漂亮的方法来状态重置为未修改当实体是不变的:

 公共静态无效CheckIfModified(EntityObject实体,ObjectContext的上下文中)
{
如果(entity.EntityState == EntityState.Modified)
{
ObjectStateEntry状态= context.ObjectStateManager .GetObjectStateEntry(实体);
DbDataRecord原稿= state.OriginalValues​​;
CurrentValueRecord CURR = state.CurrentValues​​;

布尔改变= FALSE;为
(INT I = 0; I&下; orig.FieldCount&放大器;&放大器;!改变++ⅰ)
{
对象origValue = orig.GetValue(ⅰ);
对象curValue = curr.GetValue(ⅰ );
如果(origValue.Equals(curValue!)及&放大器; (!(origValue为byte [])||((字节[])origValue).SequenceEqual((字节[])curValue)!))
{
改变= TRUE;
}
}

如果(改变!)
{
state.ChangeState(EntityState.Unchanged);
}
}
}

请注意,这种方法EF为4.0,而不是较新版本的的DbContext。但它是没有问题的重写它使用EF 4.1及以上版本,我已经这样做了自己,但现在我找不到代码。


i use the EF5 and don't know why a entity has the state "modified" after i set the only changed PropertyValue of this entity back to the original value.

using (TestDbContext context = new TestDbContext())
        {
            string name = context.Person.First().Name;

            // count is 0
            int count = context.ChangeTracker.Entries().Count(e => e.State == EntityState.Modified);

            // Change Value
            context.Person.First().Name = "Test";

            // count is 1
            count = context.ChangeTracker.Entries().Count(e => e.State == EntityState.Modified);

            // Revert Value
            context.Person.First().Name = name;


            context.ChangeTracker.DetectChanges();

            // count is 1
            count = context.ChangeTracker.Entries().Count(e => e.State == EntityState.Modified);
        }

Why? :(

解决方案

Because Entity Framework only keeps track if the data got modified, not if it's different from it's original content.

We use a nifty method to reset the state to unmodified when the entity is unchanged:

    public static void CheckIfModified(EntityObject entity, ObjectContext context)
    {
        if (entity.EntityState == EntityState.Modified)
        {
            ObjectStateEntry state = context.ObjectStateManager.GetObjectStateEntry(entity);
            DbDataRecord orig = state.OriginalValues;
            CurrentValueRecord curr = state.CurrentValues;

            bool changed = false;
            for (int i = 0; i < orig.FieldCount && !changed; ++i)
            {
                object origValue = orig.GetValue(i);
                object curValue = curr.GetValue(i);
                if (!origValue.Equals(curValue) && (!(origValue is byte[]) || !((byte[])origValue).SequenceEqual((byte[])curValue)))
                {
                    changed = true;
                }
            }

            if (!changed)
            {
                state.ChangeState(EntityState.Unchanged);
            }
        }
    }

Please note that this method is for EF 4.0, not for the newer versions with DbContext. But it is no problem to rewrite it to use EF 4.1+, I have done this myself already but I can't find the code right now.

这篇关于实体框架5 - 为什么实体状态&QUOT;可进行修改或QUOT;后为PropertyValue重新设置为原始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 23:46