Raven DB - Have it Auto Generate its own Key

元气小坏坏 提交于 2019-12-06 02:44:49

问题


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
}

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.

EDIT:

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

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


回答1:


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


来源:https://stackoverflow.com/questions/8767273/raven-db-have-it-auto-generate-its-own-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!