Back to basics: POCO and One to Many relationships (and db4o)

Here is another back to basics post. Howto ‘do’ one-to-many when you are not having a RDBM and using POCO?

Here is the (very simple) model:

For persistance I use Db4o in this example. Please see this blogpost.

One blogentry has many comments. So, we’ll put comments in a List as a attribute of blogentry:

public class BlogEntry
public string blogtitle { get; set; }
public string blogstory { get; set; }
public DateTime postdate { get; set; }
public List<Comment> comments { get; set; }

public BlogEntry()
this.postdate = DateTime.Now;


Here is the comment class:

public class Comment
public string commenttext { get; set; }
public string email { get; set; }
public BlogEntry blogentry { get; set; }


Now we are ready to build our console based blog application:

static void Main(string[] args)
using (var s = SessionFactory.Current)

var b = new BlogEntry { blogtitle = "Lady Gaga FTW",
blogstory = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book"

var c = new Comment { blogentry = b, commenttext = "inderdaad", email = "" };
var d = new Comment { blogentry = b, commenttext = "niet mee eens", email = "" };

List<Comment> commentlist = new List<Comment>();
b.comments = commentlist;


//RETRIEVE (2 foreach loops)
var blog = s.All<BlogEntry>();
foreach (BlogEntry ble in blog) {
foreach (Comment cmt in ble.comments)
Console.WriteLine("Comment: ");

So, when I execute this application a few times, the db40 file gets a bit stuffed and I get the following output, which is a full blown console blog application:

Oh well, I said it was a back to basics post, didn’t I. šŸ™‚

5 Replies to “Back to basics: POCO and One to Many relationships (and db4o)”

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *

%d bloggers liken dit: