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