Raven DB – 让它自动生成自己的密钥

我目前有一个具有名为Id的公共属性的对象。 当我存储对象时,我希望Id成为数据的一部分,而不是像目前那样成为文档ID。 创建文档存储时,我只设置连接字符串。

using (var session = documentStore.OpenSession()) { session.Store(a); session.SaveChanges(); } a can be thought of an object as: public class A { public Guid Id { get; set; } //other properties } 

所以要么我想要它生成一个随机Id,要么让数据和documentId都是A类的Id属性。

编辑:

所以有两种可能:1。文件ID = Id和

 Data Section contains: { data //sorry if the notation is not correct, doing it off of memory { Id: [my guid] //other properities } } 

或者包含Id和Document ID的数据=由raven随机生成

如果您希望Id成为您自己控制的属性并且乌鸦使用另一个属性作为文档ID,则可以使用此方法:

 public class User { public string DocumentId { get; set; } public Guid Id { get; set; } public string Name { get; set; } public User() { Id = Guid.NewGuid(); } } documentStore.Conventions.FindIdentityProperty = prop => { if (prop.DeclaringType == typeof(User)) return prop.Name == "DocumentId"; return prop.Name == "Id"; };