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

标签 c# ravendb

我开始尝试使用 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/

相关文章:

c# - 最小 API - 如何在静态类中使用 ILogger

c# - Visual Studio 在构建后显示虚假错误

c# - 文档设计 - 更新评论

ravendb - 如何在 RavenDB 文档中为嵌套属性指定 'includes'

c# - 将文档从 MVC 网页保存到第二个 RavenDb

c# - 使用 MonoMac 创建一个简单的 NSOutlineView 数据源

c# - .Net WebApi 中的多个等待异步

c# - GRASP 的 Controller 到底是什么?

c# - RavenDB 多态索引

authentication - ServiceStack - UserAuth "Id"是 INT,而 RavenDb 预计 "Id"是 STRING