6

我们有一个使用 EF4 作为其数据访问层的 ASP.Net MVC 应用程序,并且我们看到了与 OptimisitcConcurrencyExceptions 相关的意外行为,而我们认为它们应该不会被抛出。

我们已将问题简化为以下代码...

   using System.Linq;
    using Project.Model;

    namespace OptimisticConcurrency
    {
        class Program
        {
            static void Main()
            {
                Contact firstContact = null;
                using (var firstEntities = new ProjectEntities())
                {
                    firstContact = (from c in firstEntities.Contacts 
                       where c.LastName == "smith" select c).Single();
                }

                using (var secondEntities = new ProjectEntities())
                {
                    var secondContact = (from c in secondEntities.Contacts 
                       where c.LastName == "smith" select c).Single();

                    secondContact.Title = "a";
                    secondEntities.SaveChanges();
                }

                firstContact.Title = "b";

                using (var thirdEntities = new ProjectEntities())
                {
                    var thirdContact = (from c in thirdEntities.Contacts 
                       where c.LastName == "smith" select c).Single();

                    thirdContact.Title = firstContact.Title;

                    //EXPLICITLY SET VERSION HERE
                    thirdContact.Version = firstContact.Version;  

                    thirdEntities.SaveChanges();
                }
            }
        }
    }

这是我们的 MVC 应用程序中发生的事情的一个相当简单的版本,但是会出现同样的问题。

当我们在第三个实体上调用 SaveChanges 时,我预计会出现异常并且没有抛出任何东西。

更有趣的是,当我们附加 SQL Profiler 时,我们看到 Version 正在 where 子句中使用,但使用的是 thirdEntities 版本值(数据库中的当前值),而不是 firstEntities 值,尽管它被立即显式设置在调用 SaveChanges 之前。SaveChanges 将 Version 重置为检索到的值而不是设置值。

在 EDMX 中,版本设置为将 StoreGeneratedPattern 设置为 Computed。

有人知道这里发生了什么吗?

4

1 回答 1

10

这是个问题。一旦将列设置为,Computed您就无法在应用程序中设置其值(可以但不使用该值)。

编辑:

如果您从数据库加载实体,则默认情况下会使用上下文进行跟踪。上下文存储其原始值。例如,原始值用于快照更改跟踪,但它们也用作唯一有效的Computed属性来源。如果您Computed在实体中设置属性,则不使用该值并使用原始值。解决方法是修改原始值(在修改其他任何内容之前):

using (var context = new TestEntities())
{
    var entityToUpdate = context.MyEntities.Single(e => e.Id == someId);
    entityToUpdate.Timestamp = entity.Timestamp;

    ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(entityToUpdate);
    entry.ApplyOriginalValues(entityToUpdate);

    // set modified properties
    context.SaveChanges();
}

编辑2:

顺便提一句。一旦你有实际加载的时间戳和以前检索的时间戳,你可以简单地在你的应用程序中比较它们,而不是在数据库中进行比较。

于 2011-03-16T15:42:33.430 回答