1

我有这两个类:

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类一样具有身份密钥?

还有一个问题:有没有一种方法可以在不使用 post 的情况下获取评论对象。像这样的东西:

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();
}
4

1 回答 1

0

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

于 2013-03-03T04:07:40.720 回答