gpt4 book ai didi

c# - RavenDB 获取新存储文档的 ID

转载 作者:太空狗 更新时间:2023-10-30 00:19:36 27 4
gpt4 key购买 nike

我开始尝试使用 RavenDB(到目前为止看起来真的很棒)。但是我一直在尝试以下操作。

我这样存储一个新文档

Product p = new Product() { Title = "My Fancy Product" };
RavenSession.Store(p);

现在我想获取新存储的文档的 Id。谁能做到这一点?只是在商店似乎无法正常工作后访问 p.Id ...

提前致谢!

最佳答案

Product 类的 Id 属性必须是 string 类型而不是整数。

然后您将能够在操作后检索自动生成的 id:

Product p = new Product() { Title = "My Fancy Product" };
RavenSession.Store(p);
string id = p.Id;

更多信息可以在 documentation 中找到(文档 ID 部分):

In the example above we had a string Id property for BlogPost, and left it blank. It is this property that will be used as the "primary key" for this document. Note how RavenDB generated an ID for us, "BlogPosts/1", based on the default convention which we will discuss in a second.

If there is no Id property on a document, RavenDB will still generate a unique ID, but it will be retrievable only by calling session.Advanced.GetDocumentId(object). In other words, having an Id property is entirely optional, so you can explicitly define such a property only when you need this information to be more accessible.

关于c# - RavenDB 获取新存储文档的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18660024/

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