gpt4 book ai didi

c# - Put Attempted on document using non current Etag 错误

转载 作者:行者123 更新时间:2023-11-30 16:49:48 26 4
gpt4 key购买 nike

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

最初,我通过本地 API(不是通过代码)在 Raven 中创建产品记录。我为记录提供了一个手动 ID,并为每个产品创建了 json 字段和值——总共 9 个。然后我编写了加载这些产品的应用程序部分,一切正常。每次返回的数据都完全符合我的预期。

但是,当我到达允许用户通过 MVC 接口(interface)创建新记录的应用程序部分时,我在调用 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;
}
}

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

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

但是,我不明白为什么在我的 9 次崩溃中途修改 Hilo 并没有使 Raven 开始在 Products/65 生成,而是一直尝试生成已经使用的 ID。

最佳答案

之所以这样做,是因为它在缓存 hilo 值,在用完 hilo 范围之前,它没有理由请求新的范围。hilo 不会在每次请求时都转到服务器。

关于c# - Put Attempted on document using non current Etag 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35927546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com