2016-12-27 75 views
0

使用烏鴉客戶端和服務器#30155。我基本上做以下的控制器:如何加載文件從數據庫中,而不是存儲

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 
} 

然後在document.Update,我試着這樣做:

var old = store.Load<T>(this.Id); 

if (old.Date != this.Date) 
{ 
    // Resolve conflicts that occur by moving document period 
} 

store.Update(this); 

現在,我遇到的問題是old被載入了內存而不是數據庫並且已經包含更新的值。因此,它永遠不會進入衝突檢查。

我試圖通過改變Controller.Update方法爲解決該問題:

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 
    } 

這使得我越來越帶有文本Raven.Client.Exceptions.NonUniqueObjectExceptionAttempted to associate a different object with id

現在,問題:

  1. 如果我嘗試爲什麼烏鴉照顧和id爲一個新的對象,只要關聯的新對象進行適當的電子標籤和類型?

  2. 是否有可能加載一個文件在其數據庫中的狀態(覆蓋默認行爲,如果它存在有從內存中讀取文件)?

  3. 什麼是使document.Update()工作的最佳解決方案(最好不必傳遞舊對象)?

回答

0
  1. ,如果我嘗試一個新的對象與ID只要新對象進行適當的電子標籤和類型相關聯爲什麼烏鴉照顧?

RavenDB傾向於能夠從內存(這是更快)的文件服務。通過檢查保持對象爲同一ID,很難調試錯誤阻止。

編輯:請參見下面Rayen的評論。如果您在Store中啓用併發檢查/提供etag,則可以繞過該錯誤。

  1. 是否可以加載處於其數據庫狀態的文檔(覆蓋默認行爲以從存儲器中獲取文檔,如果存在的話)?

可能沒有。

  1. 什麼是讓document.Update()工作(最好不必傳遞舊對象)的好方法?

我去重構文檔。更新方法也有一個可選參數來接收舊的日期時間段,因爲#1和#2似乎不可能。

-1

RavenDB支持開箱即用的樂觀併發。你需要做的唯一一件事情就是調用它。

session.Advanced.UseOptimisticConcurrency = true; 

參見: http://ravendb.net/docs/article-page/3.5/Csharp/client-api/session/configuration/how-to-enable-optimistic-concurrency

+0

我可能會忽略一些東西,但這與我的問題有什麼關係?我沒有嘗試在多個會話中編輯同一個文檔,我在多個會話中加載文檔,但僅在一個會話中進行變更。將此屬性設置爲true不會阻止「NonUniqueObjectException」,並且它無助於事實I無法獲得文檔的實際數據庫版本。 – Chrotenise

+0

在這種情況下,請保留舊會話的etag,然後調用Store(entity,etag);這將強制併發檢查服務器端。 –

相關問題