在ravenDB中将标识键添加到子类

时间:2013-03-02 15:45:38

标签: ravendb

我有这两节课:

public class BlogPost
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Category { get; set; }
    public string Content { get; set; }
    public DateTime PublishedAt { get; set; }
    public string[] Tags { get; set; }
    public BlogComment[] Comments { get; set; }
}

public class BlogComment
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
}

并添加到这样的文档:

// Creating a new instance of the BlogPost class
BlogPost post = new BlogPost()
                    {
                        Title = "Hello RavenDB",
                        Category = "RavenDB",
                        Content = "This is a blog about RavenDB",
                        Comments = new BlogComment[]
                                    {
                                        new BlogComment() {Title = "Unrealistic", Content = "This example is unrealistic"},
                                        new BlogComment() {Title = "Nice", Content = "This example is nice"}
                                    }
                    };

有没有一种方法可以让我的评论像我的 BlogPost 类一样拥有身份密钥?

和另一个问题: 有没有办法获得评论对象而不使用帖子。像这样的东西:

using( var session = doc.OpenSession() )
{
    return session.Load<BlogComment>( ID );
}

using( var session = doc.OpenSession() )
{
     return ( from comment in session.Query<BlogComment>()
     where comment.Title == title
     select comment ).FirstOrDefault();
}

1 个答案:

答案 0 :(得分:0)

您可以在BlogPost上拥有一个整数属性,增加该属性并在添加新注释时设置该值。这样可以在帖子的范围内为您提供身份样式ID。