title: How to do Cascading Include in LiteDB
body: <p>here is example on how to store cross-referenced entities in LiteDB. LiteDB stores the cross-referenced entities perfectly fine, but problem comes when I am trying to find/load entities back. My goal is NOT ONLY the requested entity but also referenced ones. There is quick tutorial section "DbRef for cross references" on LiteDB <a href="http://www.litedb.org/" rel="nofollow">webpage</a> how one can realize it. LiteDB has "Include" option (which is called before "FindAll") which says which referenced entities must be loaded as well. I am trying to achieve it in this code example but with no results, i.e, the code raises Exception("D_Ref") meaning "D_Ref" reference is not loaded: </p>
  
  <pre><code>namespace _01_simple {
  using System;
  using LiteDB;
  
  public class A {
      public int Id { set; get; }
      public string Name { set; get; }
      public B B_Ref { set; get; }
  }
  public class B {
      public int Id { set; get; }
      public string Name { set; get; }
      public C C_Ref { set; get; }
  }
  public class C {
      public int Id { set; get; }
      public string Name { set; get; }
      public D D_Ref { set; get; }
  }
  public class D {
      public int Id { set; get; }
      public string Name { set; get; }
  }
  
  class Program {
      static void Main(string[] args) {
          test_01();
      }
  
      static string NameInDb&lt;T&gt;() {
          var name = typeof(T).Name + "s";
          return name;
      }
  
      static void test_01() {
          if (System.IO.File.Exists(@"MyData.db"))
              System.IO.File.Delete(@"MyData.db");
  
          using (var db = new LiteDatabase(@"MyData.db")) {
              var As = db.GetCollection&lt;A&gt;(NameInDb&lt;A&gt;());
              var Bs = db.GetCollection&lt;B&gt;(NameInDb&lt;B&gt;());
              var Cs = db.GetCollection&lt;C&gt;(NameInDb&lt;C&gt;());
              var Ds = db.GetCollection&lt;D&gt;(NameInDb&lt;D&gt;());
  
              LiteDB.BsonMapper.Global.Entity&lt;A&gt;().DbRef(x =&gt; x.B_Ref, NameInDb&lt;B&gt;());
              LiteDB.BsonMapper.Global.Entity&lt;B&gt;().DbRef(x =&gt; x.C_Ref, NameInDb&lt;C&gt;());
              LiteDB.BsonMapper.Global.Entity&lt;C&gt;().DbRef(x =&gt; x.D_Ref, NameInDb&lt;D&gt;());
  
              var d = new D { Name = "I am D." };
              var c = new C { Name = "I am C.", D_Ref = d };
              var b = new B { Name = "I am B.", C_Ref = c };
              var a = new A { Name = "I am A.", B_Ref = b };
  
              Ds.Insert(d);
              Cs.Insert(c);
              Bs.Insert(b);
              As.Insert(a);
          }
  
          using (var db = new LiteDatabase(@"MyData.db")) {
              var As = db.GetCollection&lt;A&gt;(NameInDb&lt;A&gt;());
  
              var all_a = As
                  .Include(x =&gt; x.B_Ref)
                  .FindAll();
              foreach (var a in all_a) {
                  if (a.B_Ref == null)
                      throw new Exception("B_Ref");
                  if (a.B_Ref.C_Ref == null)
                      throw new Exception("C_Ref");
                  if (a.B_Ref.C_Ref.D_Ref == null)
                      throw new Exception("D_Ref");
              }
          }
      }
  }}
  </code></pre>