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

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

                                }
                };

Persisting this will result in a BlogComment field being embedded in The BlogPost document

RavenDB Will not generate the Id for the BlogComment Field

What i actually want is for BlogPost to Hold just a reference to the BlogComment Field and for the BlogComment instance to be stored as a seperate entity

Any hint will do

thanks. I had to edit to Correctly represent my predicament

Adrian Thompson Phillips
  • 6,893
  • 6
  • 38
  • 69
Paul Plato
  • 1,471
  • 6
  • 28
  • 36

2 Answers2

3

RavenDB is not a relational database, so if you go into it expecting to get the same behavior as SQL when it comes to object relationships you are gonna have a real bad time. Based on your edit, you need to store the Comment as a separate entity with its own string Id property. Then you can store that on a property of your BlogPost object. However, you really don't want to update the BlogPost document every time anyone adds a comment, so a better solution is to store the ID for the BlogPost on the Comment object, since that properly represents the relationship and uses the document model correctly. Then you will load a BlogPost and all the Comment documents all based on the one BlogPost ID.

RyanR
  • 7,728
  • 1
  • 25
  • 39
  • That's not the only way to model blog comments - see for example how RaccoonBlog does it – synhershko Oct 23 '13 at 14:48
  • @synhershko I will give you $100 if you can point me at "the only way to do it" for _any_ problem in software development. I was simple giving him a solution that is a good step for someone that is obviously just learning Raven. And as for Ayende, well, his solution to any given problem changes depending on how many shepards pies he's had (this is a personal experience anecdote) – RyanR Oct 23 '13 at 14:51
0

Something like this (document IDs in RavenDB are strings, hence so are references):

public class Ex1
{
  public string Ex2Id {get;set;}
}

Just make sure you're modeling things correctly, in the document-oriented mindset and not trying to mimic relational behavior

synhershko
  • 4,472
  • 1
  • 30
  • 37