【问题标题】:Raven DB - Have it Auto Generate its own KeyRaven DB - 让它自动生成自己的密钥
【发布时间】:2012-01-07 04:07:06
【问题描述】:

我当前有一个对象,它具有一个名为 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,要么让数据和 documentId 都是 A 类的 Id 属性。

编辑:

所以有两种可能: 1. Document 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应该是什么样的?文档应该是什么样子?

标签: c# .net ravendb


【解决方案1】:

如果您希望 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";
};

【讨论】:

  • 此方法是否还会将 Name 属性保存在 Document 的 Data 部分中?
  • 肯定会的。布拉德,如果你有 nuget,就很容易尝试这些事情。
猜你喜欢
  • 2013-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-16
  • 2018-06-03
  • 1970-01-01
  • 2022-11-16
  • 2016-03-07
相关资源
最近更新 更多