OptimisticConcurrencyException在未更改的对象上使用自跟踪实体 [英] OptimisticConcurrencyException using self tracking entities on an unchanged object

查看:129
本文介绍了OptimisticConcurrencyException在未更改的对象上使用自跟踪实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无法保存到数据库的小对象图。  情况类似于:


 



  • 一个学生对象,其中包含对学校对象的引用(多对一)
  • 进行了更改到学生对象。  在我的范围之外,对学校对象进行了更改。
  • 学校状态不变,学生状态被修改。  我将ApplyChanges和SaveChanges调用到student对象。  抛出乐观并发异常。   OptimisticConcurrencyException的
    StateEntries中的对象是学校对象。

我假设如果对象状态未更改,则不应进行并发检查是吗?

解决方案

你提到学校对象在你的范围之外用另一个objectcontext更改,所以你的范围的学校对象不变,
当ConcurrencyMode属性是固定后,实体框架会在保存对数据库的更改之前检查数据库中的更改,并且任何冲突的更改都将导致 
OptimisticC oncurrencyException


I have a small object graph that fails to save to the database.  The situation is similar to:

 

  • A student object which has a reference to a school object (many to one)
  • A change is made to the student object.  Outside my scope, a change is made to the school object.
  • The school state is unchanged, the student state is modified.  I call ApplyChanges and SaveChanges to the student object.  An optimistic concurrency exception is thrown.  The object in the OptimisticConcurrencyException's StateEntries is the school object.

I assumed that if an objects state is unchanged, no concurrency check should be occurring?

解决方案

You mentioned that the school object is changed outside of your scope with another objectcontext, so althrough the school object of your scope is unchanged, when ConcurrencyMode attribute is fixed, the Entity Framework checks for changes in the database before saving changes to the database and any conflicting changes will cause an OptimisticConcurrencyException.


这篇关于OptimisticConcurrencyException在未更改的对象上使用自跟踪实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆