如何在ravendb中设置uniqueid

时间:2012-12-20 22:20:33

标签: c# ravendb

我正在学习RavenDB并熟悉工作原理。我不明白的一件事是如何将raven创建的id保存到属性中。

public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string ID { get; set; } //This will be null when I save object to DB I want it to be same as generated
}

[TestMethod]
public void TestSaving()
{
    using (var session = documentStore.OpenSession())
    {
        var user = new User { FirstName = "John", LastName = "Doe" };
        session.Store(user);
        session.SaveChanges(); //I want that property to save that id that raven created: Users/65
    }
}

2 个答案:

答案 0 :(得分:6)

我认为Raven中用于查找标识属性的默认约定是“Id”并且区分大小写。尝试将您的属性重命名为Id,而不是ID。

答案 1 :(得分:1)

再看一遍。在致电session.Store(user)后,您会立即发现ID属性已经为您设置。甚至在调用SaveChanges()之前。

<强>更新

对不起,我没有注意到你房产的外壳。 Raven的默认约定是查找名为“Id”的属性 - 这是区分大小写的。如果你想要一个不同的约定,你可以改变乌鸦的默认值:

documentStore.Conventions.FindIdentityProperty = x => x.Name == "ID";

您可以阅读有关默认约定here

的更多信息
相关问题