EntityState.Unchanged与EntityState.Modified没有实际修改

时间:2013-10-23 04:32:56

标签: entity-framework-5

使用EF5 winforms,我想弄清楚数据是否在我的上下文中发生了变化。 我的问题是,如果调用了DetectChanges,EntityState.Modified将为true,无论是否有任何值发生变化。

要执行此操作,我使用以下代码段

 foreach (var e in Db.ChangeTracker.Entries())
        {
            switch (e.State)
            {
                case EntityState.Unchanged :
                    break;
                case EntityState.Deleted:
                    break;
                case EntityState.Added:
                    break;
                case EntityState.Modified:
                    var original = this.enumerateOriginalValues(e);
                    var current = this.enumerateCurrentValues(e);
                    if (original == current) sChanges = "No Changes";
                    else sChanges = string.Format("Was: {0}  Is: {1}", original, current);
                    break;
            }
            // build string indicating No Changes or UnChanged
            s = string.Format("{0} \r\n {1} {2} {3},{4}", s, sType, sKey, sState, sChanges); 
        }

有没有更好的方法来确定上下文是否有变化?

[更新] 在格特的建议下,我在一个二传手的房产中休息了一下。我在调用堆栈中找到了以下过程。 qry.Load行导致setter触发。

  public override void Refresh()
    {
        try
        {
            try
            {
                if (base.bindingSource.DataSource != null)
                {
                    var per = (Person)base.bindingSource.Current;

                    this.SaveChangesIfNeeded();
                }

                DbSet<Person> dset = base.Context.People;
                if (this.QuickSearch == null)
                {
                    this.QuickSearch ="";
                }

                IQueryable<Person> qry;
                if (this._organisationId == 0)
                {
                    qry =
                        dset.Where(
                            p => p.LastName.Contains(this.QuickSearch) || p.FirstName.Contains(this.QuickSearch));
                }
                else
                {
                    qry =
                        dset.Where(
                            p =>
                            (p.LastName.Contains(this.QuickSearch) || p.FirstName.Contains(this.QuickSearch)) &&
                            (p.Organisation.Id == this._organisationId));
                }

                qry.Load();  // Setter is called here
                base.bindingSource.DataSource = dset.Local.ToBindingList();
            }
            catch (Exception ex)
            {
                DialogResult dialogResult = MessageBox.Show(ex.Message);
                throw;
            }
        }
        catch (Exception ex)
        {
            HandleException.Show(ex);
        }
    }

[update]单击导航按钮时也会发生中断。这是调用堆栈。

>   SBD.Syrius.DomainClasses.dll!SBD.Syrius.DomainClasses.Person.Title.set(string value) Line 537   C#
[Native to Managed Transition]  
System.dll!System.ComponentModel.ReflectPropertyDescriptor.SetValue(object component, object value) + 0x197 bytes   
System.Windows.Forms.dll!System.Windows.Forms.BindToObject.SetValue(object value) + 0x8a bytes  
System.Windows.Forms.dll!System.Windows.Forms.Binding.PullData(bool reformat, bool force) + 0x26e bytes 
System.Windows.Forms.dll!System.Windows.Forms.BindingManagerBase.PullData(out bool success) + 0x118 bytes   
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.CurrencyManager_PullData() + 0x2f bytes   
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.EndCurrentEdit() + 0x27 bytes 
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.ChangeRecordState(int newPosition, bool validating, bool endCurrentEdit, bool firePositionChange, bool pullData) + 0xf3 bytes 
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.Position.set(int value) + 0x5b bytes  
System.Windows.Forms.dll!System.Windows.Forms.BindingNavigator.OnMoveNext(object sender, System.EventArgs e) + 0x2b bytes   
System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.HandleClick(System.EventArgs e) + 0xb6 bytes    
System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.HandleMouseUp(System.Windows.Forms.MouseEventArgs e) + 0x237 bytes  
System.Windows.Forms.dll!System.Windows.Forms.ToolStrip.OnMouseUp(System.Windows.Forms.MouseEventArgs mea) + 0xef bytes 
System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button, int clicks) + 0x48b bytes 
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) + 0xe14 bytes 
System.Windows.Forms.dll!System.Windows.Forms.ToolStrip.WndProc(ref System.Windows.Forms.Message m) + 0x8c bytes    
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) + 0x14c bytes    
[Native to Managed Transition]  
[Managed to Native Transition]  
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData) + 0x681 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) + 0x57c bytes  
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x6f bytes    
SBD.Syrius.UI.exe!SBD.Syrius.UI.Program.Main(string[] args) Line 57 + 0x9 bytes C#
[Native to Managed Transition]  
[Managed to Native Transition]  
mscorlib.dll!System.Runtime.Hosting.ApplicationActivator.CreateInstance(System.ActivationContext activationContext, string[] activationCustomData) + 0x66 bytes 
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() + 0x8d bytes   
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x285 bytes 
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x9 bytes   
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x57 bytes    
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x51 bytes   
[Native to Managed Transition]  

0 个答案:

没有答案