2016-03-10 40 views
3

我正在閱讀流行的Pro ASP.NET MVC 5書籍並構建Sports Store應用程序,但我使用的是RavenDb而不是實體框架。把試圖放在使用非當前Etag錯誤的文檔上

最初,我通過本地API(不通過代碼)在Raven中創建了產品記錄。我給記錄一個手動ID,併爲每個產品創建了json字段和值 - 總共爲9個。然後,我編寫了加載這些產品的應用程序的各個部分,並且一切正常。數據完全按照我的預期每次返回。但是,當我到了允許用戶通過MVC接口創建新記錄的應用程序部分時,在IDocumentSession上調用SaveChanges()函數時發生崩潰。

這是錯誤的全文:

Server Error in '/' Application. 

PUT attempted on document 'products/9' using a non current etag 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: Raven.Abstractions.Exceptions.ConcurrencyException: PUT attempted on document 'products/9' using a non current etag 

這是保存功能我打電話的全文:

private readonly IDocumentSession _session; 
    public void Save(Product product) 
    { 
     _session.Store(product);    
     _session.SaveChanges(); 
    } 

_session在這樣的庫構造初始化:

public ProductRepository() 
    { 
     _session = RavenDbStoreSingleton.DocumentStore.OpenSession(); 
    } 

而這一切都與DocumentStore的單件相互作用:

public class RavenDbStoreSingleton 
{ 
    private static readonly Lazy<IDocumentStore> Store = new Lazy<IDocumentStore>(CreateStore); 

    public static IDocumentStore DocumentStore => Store.Value; 

    private static IDocumentStore CreateStore() 
    { 
     IDocumentStore store = new DocumentStore() 
     { 
      Url = "http://localhost:8080", 
      DefaultDatabase = "SportsStore" 
     }.Initialize(); 
     return store; 
    } 
} 

所以我的猜測是,我的手動輸入所有的第一個記錄,然後試圖挽救某種方式導致該錯誤的新記錄,但我不知道爲什麼烏鴉可以負載產品,但不能似乎保存沒有碰到衝突的ID。我甚至將產品(這是唯一的文檔類型)的Hilo從32增加到64,但這並不影響任何內容。

更新:我注意到,每次出現此錯誤時,它都會在比以前更高的Id上發生衝突。我能夠不斷嘗試保存,直到錯誤不再發生,此時Id生成的是Products/65,這很有意義,因爲我已將Hilo從32修改爲64.它確實是而不是嘗試生成產品/ 10。

但是,我不明白爲什麼通過我的9次崩潰中途修改Hilo並沒有讓Raven開始在Products/65上生成,而是一直試圖生成已經被使用的Ids。

回答

2

它之所以這樣做是因爲它緩存了hilo值,直到它耗盡hilo範圍,它沒有理由要求新的範圍。 hilo不會在每次請求時都轉到服務器。

+0

除了我已多次重新啓動測試應用程序 - 我是否還需要重新啓動本地IIS實例才能解決此錯誤? – Max

+0

有沒有解決方法,我自己遇到同樣的問題。 – CodingGorilla

相關問題