当一个用户更新已经被另一个用户更新的时候,我应该如何解决模拟更新?
第一个用户请求‘类别’实体对象,第二个用户也这样做。
第二个用户更新这个对象,第一个用户更新。
我有字段时间戳字段在数据库中,wa设置为并发模式-固定。
我是这样更新的:
public class CategoriesRepository : BaseCategoryRepository
{
public void Update(....)
{
try
{
Category catToUpdate = (from c in Contentctx.Categories where c.CategoryID == categoryId select c).FirstOrDefault();
catToUpdate.SectionReference.EntityKey = new System.Data.EntityKey("ContentModel.Sections", "SectionID", sectionId);
if (catToUpdate != null)
{
//Set fields here....
Contentctx.SaveChanges();
}
base.PurgeCacheItems(CacheKey);
}
catch (OptimisticConcurrencyException ex)
{
}
}
}
//Contentctx comes from base class: Contentctx:
private ContentModel _contenttx;
public ContentModel Contentctx
{
get
{
if ((_contenttx == null))
{
_contenttx = new ContentModel();
}
return _contenttx;
}
set { _contenttx = value; }
}
//on Business layer:
using (CategoriesRepository categoriesRepository = new CategoriesRepository())
{
categoriesRepository.UpdateCategory(.....);
}例外从不跳跃..。
我该怎么处理呢?
发布于 2011-01-15 13:13:02
您确定按您描述的那样执行调用顺序吗?无论如何,这里的主要问题是您不应该使用Upate方法中的查询返回的时间戳。当用户获得用于更新的初始数据时,您应该使用收到的时间戳。它应该是:
processin
这是因为它可以在第一次调用和更新方法调用之间传递大量时间,因此可能已经处理了几个更改。您必须使用初始时间戳以避免无声地覆盖其他更改。
编辑:
// You are updating category - update probably means that you had to load category
// first from database to show actual values. When you loaded the category it had some
// timestamp value. If you don't use that value, any change between that load and c
// calling this method will be silently overwritten.
public void Update(Category category)
{
try
{
Category catToUpdate = (from c in Contentctx.Categories where c.CategoryID == categoryId select c).FirstOrDefault();
...
// Now categoryToUpdate contains actual timestamp. But it is timestamp of
// actual values loaded now. So if you use this timestamp to deal with
// concurrency, it will only fire exception if somebody modifies data
// before you call SaveChanges few lines of code bellow.
if (catToUpdate != null)
{
//Set fields here....
// To ensure concurrency check from your initial load, you must
// use the initial timestamp.
catToUpdate.Timestamp = category.Timestamp;
Contentctx.SaveChanges();
}
...
}
catch (OptimisticConcurrencyException ex)
{
...
}
} https://stackoverflow.com/questions/4699252
复制相似问题