Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top