使用实体框架解决乐观并发更新问题
当一个用户更新另一用户已经更新的条目时,我应该如何解决模拟更新?
第一个用户请求“类别”实体对象,第二个用户执行相同操作。
第二个用户更新此对象,第一个用户更新。
我在数据库中有字段时间戳字段,该字段设置为并发模式 - 已修复。
这就是我更新的方式:
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(.....);
}
异常永远不会跳转...
我应该如何处理这个问题?
How should I solve simulation update, when one user updates already updated entery by another user?
First user request 'Category' entityobject, second user does the same.
Second user updates this object and first user updates.
I have field timestamp field in database that wa set to Concurrency Mode - Fixed.
This is how I update:
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(.....);
}
Exception never jumps...
How should I handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定调用顺序按照您的描述执行吗?无论如何,这里的主要问题是您不应该在 Upate 方法中使用查询返回的时间戳。您应该使用用户获取初始数据进行更新时收到的时间戳。它应该是:
这样做的原因是,它可能会在第一次调用和更新方法调用之间花费大量时间,因此可能已经处理了多个更改。您必须使用初始时间戳来避免无提示地覆盖其他更改。
编辑:
Are you sure that sequence of calls is performed as you described? Anyway the main problem here is that you should not use the timestamp returned by the query in Upate method. You should use timestamp received when user get initial data for update. It should be:
The reason for this is that it can pass a lot of time between first call and update method call so there can be several changes already processed. You have to use initial timestamp to avoid silent overwritting other changes.
Edit: