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



我当前有一个对象,该对象有一个名为Id的公共属性。当我存储该对象时,我希望Id成为Data的一部分,而不是像当前那样成为文档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,要么让数据和文档Id都是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
    }
}

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

如果您希望Id是您自己控制的属性,并且让raven使用另一个属性作为文档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";
};

最新更新