title: Reading Related Entites with EntityFramework Core (3.1.11) and SQLite
body: <p>I've found similar questions posted online, but none of them solved my problem, so I decided to describe it in details here.
  Here's a simple One-To-Many relationship created with EF Core:</p>
  <pre class="lang-ml prettyprint-override"><code>type Parent() =
      member val ParentId = Guid()                with get, set
      member val Name = &quot;&quot;                        with get, set
      member val Relatives = new List&lt;Relative&gt;() with get, set
  
  and Relative() =
      member val RemoteKey = Guid() with get, set
      member val LocalField = &quot;&quot;    with get, set
      member val Parent = Parent()  with get, set
  
  type TestContext(fileName: string) =
      inherit DbContext()
  
      [&lt;DefaultValue&gt;] val mutable parent : DbSet&lt;Parent&gt;
      [&lt;DefaultValue&gt;] val mutable relative : DbSet&lt;Relative&gt;
  
      member this.Parent with get() = this.parent and set p = this.parent &lt;- p
      member this.Relative with get() = this.relative and set r = this.relative &lt;- r
  
      override this.OnConfiguring(optionsBuilder: DbContextOptionsBuilder) =
          optionsBuilder.UseSqlite(dbConnection) |&gt; ignore
  
      override this.OnModelCreating(builder: ModelBuilder) =
          builder.Entity&lt;Parent&gt;(fun entity -&gt; 
              entity.HasKey(fun p -&gt; p.ParentId :&gt; obj) |&gt; ignore
          ) |&gt; ignore
  
          builder.Entity&lt;Relative&gt;(fun entity -&gt;
              entity.HasKey(fun r -&gt; (r.RemoteKey, r.LocalField) :&gt; obj) |&gt; ignore
  
              entity
                  .HasOne(fun r -&gt; r.Parent)
                  .WithMany(fun p -&gt; p.Relatives :&gt; seq&lt;_&gt;)
                  .HasPrincipalKey(fun p -&gt; p.ParentId :&gt; obj)
                  .HasForeignKey(fun r -&gt; r.RemoteKey :&gt; obj)
              |&gt; ignore
          ) |&gt; ignore
  </code></pre>
  <p>The query below returns all the <code>Relative</code> records found in the db.</p>
  <pre class="lang-ml prettyprint-override"><code>use db = new TestContext(fileName)
  let relatives = 
      query {
              for r in db.Relative.Include(fun r -&gt; r.Parent) do
                  select r
            } |&gt; Seq.toArray
  // Assuming 'relatives' is not empty, this prints parent's name
  printfn &quot;ParentName: %s&quot; (relatives.[0].Parent.Name)
  </code></pre>
  <p>The query explicitly requests to include the foreign record in the query by calling <code>.Include(fun r -&gt; r.Parent)</code>.
  However, this doesn't work in the other direction:</p>
  <pre class="lang-ml prettyprint-override"><code>use db = new TestContext(fileName)
  let parents = 
      query {
              for p in db.Parent.Include(fun p -&gt; p.Relatives) do
                  select p
            } |&gt; Seq.toArray
  // Even assuming that 'parents' is not empty and has related entries in the db, this prints an empty array
  printfn &quot;Relatives: %A&quot; (parents.[0].Relatives)
  </code></pre>
  <p>The question is: how can I refer to the <code>Relative</code> records having a reference to <code>Parent</code>?</p>
  <h3>EDIT</h3>
  <p>I noticed that changing types definitions to F# Record syntax with <code>CLIMutable</code> Attribute fixes the problem. The question is: what's the difference between those two representations. I'm going to use ILSpy and will update shortly.</p>
  <pre class="lang-ml prettyprint-override"><code>// This works with .Include(fun p -&gt; p.Relatives)
  [&lt;CLIMutable&gt;]
  type Parent = 
      {
          ParentId : Guid
          Name : string
          Relatives : List&lt;Relatives&gt;
      }
  and [&lt;CLIMutable&gt;] Relative =
      {
          RemoteKey : Guid
          LocalField : string
          Parent : Parent
      }
  </code></pre>
  <h3>EDIT 2</h3>
  <p>I've worked out this is due to the fact the F# class does not allow <code>null</code> by default and my implementation is missing another constructor, that accepts all the parameters. So the complete definition that works with EF looks as follows:</p>
  <pre class="lang-ml prettyprint-override"><code>    type [&lt;AllowNullLiteral&gt;] Parent(gid: Guid, field: string, relatives: ICollection&lt;Related&gt;) =
          member val Id = gid with get, set
          member val Field = field with get, set
          member val Relative = relatives with get, set
  
          new() = Parent(Guid.NewGuid(), null, null)
  
      and [&lt;AllowNullLiteral&gt;] Relative(pid: Guid, field: string, parent: Parent) =
          member val ParentId = pid with get, set
          member val LocalField = field with get, set
          member val Parent = parent with get, set
  
          new() = Related(Guid.NewGuid(), null, null)
  </code></pre>