使用非当前Etag错误尝试使用文档

我正在阅读流行的Pro ASP.NET MVC 5书并构建Sports Store应用程序,但我使用的是RavenDb而不是Entity Framework。

最初,我通过本地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 

这是我调用的Save函数的全文:

  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 Store = new Lazy(CreateStore); public static IDocumentStore DocumentStore => Store.Value; private static IDocumentStore CreateStore() { IDocumentStore store = new DocumentStore() { Url = "http://localhost:8080", DefaultDatabase = "SportsStore" }.Initialize(); return store; } } 

所以我的猜测是我手动输入所有的第一个记录,然后尝试保存新记录以某种方式导致此错误,但我不知道为什么Raven可以加载产品但似乎无法保存而不会碰到冲突的ID。 我甚至把产品的Hilo(这是唯一的文档类型)从32增加到64,但这并没有影响任何东西。

更新 :我注意到每次出现此错误时,它都会比以前更高的ID发生冲突。 我能够继续尝试保存,直到错误不再发生,此时生成的Id是Products / 65,这是有意义的,因为我已经将Hilo从32修改为64.它没有尝试生成Products / 10。

但是,我不明白为什么在我的9次碰撞中途修改Hilo并没有让Raven开始在Products / 65中生成,而是继续尝试生成已经采用的Ids。

这样做的原因是它缓存了hilo值,直到它超出hilo范围,它没有理由要求新的范围。 hilo不会在每次请求时都进入服务器。