乌鸦DB - 有它自动生成了自己的密钥 [英] Raven DB - Have it Auto Generate its own Key

查看:152
本文介绍了乌鸦DB - 有它自动生成了自己的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个对象,它有一个名为ID的公共财产。当我存储对象,我想的标识是数据的一部分,而不是成为文档ID喜欢目前它。在创建文档存储我只设置连接字符串。

I current have an object that has a public property called Id. When I store the object, I would like the Id to be part of the Data and not become the document Id like it currently does. When creating the document store I only set the connection string.

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属性。

So either I want it to generate a random Id or have both the data and the documentId being the Id property of class A.

编辑:

所以两种可能性: 1.文档ID = Id和

So two possibilities: 1. Document Id = Id and the

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

或含有ID和数据文档ID =由乌鸦随机生成

Or the data containing the Id and Document ID = randomly generated by raven

推荐答案

您可以使用这个,如果你想要编号来是你控制你自己,并有一个属性乌鸦使用其他属性的文件ID来代替:

You can use this if you want Id to be a property that you control on your own and have raven use another property as the documents id instead:

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";
};

这篇关于乌鸦DB - 有它自动生成了自己的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆