C# 4.0 如何从数据库而不是内存中加载文档
使用Raven客户端和服务器#30155。我基本上在控制器中执行以下操作:C# 4.0 如何从数据库而不是内存中加载文档,c#-4.0,ravendb,C# 4.0,Ravendb,使用Raven客户端和服务器#30155。我基本上在控制器中执行以下操作: public ActionResult Update(string id, EditModel model) { var store = provider.StartTransaction(false); var document = store.Load<T>(id); model.UpdateEntity(document) // overwrite document propert
public ActionResult Update(string id, EditModel model)
{
var store = provider.StartTransaction(false);
var document = store.Load<T>(id);
model.UpdateEntity(document) // overwrite document property values with those of edit model.
document.Update(store); // tell document to update itself if it passes some conflict checking
}
这导致我得到了一个Raven.Client.Exceptions.ununiqueObjectException
,文本为:试图将另一个对象与id关联
现在,问题是:
document.Update()
正常工作(最好不必传递旧对象),有什么好的解决方案RavenDB支持开箱即用的乐观并发。你唯一需要做的就是给它打电话
session.Advanced.UseOptimisticConcurrency = true;
见:
我对document.Update方法进行了重构,使其具有一个可选参数来接收旧的日期周期,因为#1和#2似乎不可能。我可能忽略了一些东西,但这与我的问题有什么关系?我没有尝试在多个会话中编辑同一文档,我正在多个会话中加载文档,但仅在一个会话中发生变化。将此属性设置为true将不会阻止
UnuniqueObjectException
,并且这无助于我无法获取文档的实际数据库版本。在这种情况下,从旧会话中保留etag,然后调用Store(实体,etag);这将强制服务器端进行并发检查。
public ActionResult Update(string id, EditModel model)
{
var store = provider.StartTransaction(false);
var document = store.Load<T>(id);
store.Dispose();
model.UpdateEntity(document) // overwrite document property values with those of edit model.
store = provider.StartTransaction(false);
document.Update(store); // tell document to update itself if it passes some conflict checking
}
session.Advanced.UseOptimisticConcurrency = true;