title: C# to F# - EF Code First
body: <p>I'm having a one-to-many relationship between <code>Dealer</code> who can have many <code>Cars</code>.</p>
  
  <p>I'm trying to convert my C# code thats used for EF to F#... The problem is just that in my F# code it can fine get a Dealer, but it wont for that Dealer... it just returns null, but in the C# version it works?</p>
  
  <p>My code:</p>
  
  <p><strong>The C# version</strong></p>
  
  <pre><code>public class Dealer
  {
      public int ID { get; set; }
      public string Name { get; set; }
      public virtual ICollection&lt;Car&gt; Cars { get; set; }
  
      public Dealer()
      {
          Cars = new List&lt;Car&gt;();
      }
  }
  public class Car
  {
      public int ID { get; set; }
      public string CarName { get; set; }
      public Dealer Dealer { get; set; }
      public int DealerId { get; set; }
  
      public Car()
      {
          Dealer = new Dealer();
      }
  }
  public class MyContext : DbContext
  {
      public DbSet&lt;Dealer&gt; Dealers { get; set; }
      public DbSet&lt;Car&gt; Cars { get; set; }
      public MyContext()
      {
          Database.Connection.ConnectionString = "server=some;database=dbname;user id=uid;password=pwd";
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Dealer&gt;()
              .HasMany(x =&gt; x.Cars)
              .WithRequired(x =&gt; x.Dealer)
              .HasForeignKey(x =&gt; x.DealerId);
  
          modelBuilder.Entity&lt;Car&gt;()
              .HasRequired(x =&gt; x.Dealer)
              .WithMany()
              .HasForeignKey(x =&gt; x.DealerId);
  
          modelBuilder.Entity&lt;Dealer&gt;().ToTable("Dealers");
          modelBuilder.Entity&lt;Car&gt;().ToTable("Cars");
      }
  }
  </code></pre>
  
  <p>... Program.cs which query for dealer and his cars:</p>
  
  <pre><code>static void Main(string[] args)
  {
      var ctx = new MyContext();
      foreach (var s in ctx.Dealers.FirstOrDefault(x =&gt; x.ID == 1).Cars)
      {
          Console.WriteLine(s.CarName);
      }
  
      Console.Read();
  }
  </code></pre>
  
  <p><strong>The F# version</strong></p>
  
  <pre><code>type Dealer() =
      let mutable id = 0
      let mutable name = ""
      let mutable cars = List&lt;Car&gt;() :&gt; ICollection&lt;Car&gt;
      member x.ID with get() = id and set v = id &lt;- v
      member x.Name with get() = name and set v = name &lt;- v
      abstract member Cars : ICollection&lt;Car&gt; with get, set
      override x.Cars with get() = cars and set v = cars &lt;- v
  and Car() =
      let mutable id = 0
      let mutable carName = ""
      let mutable dealer = Dealer()
      let mutable dealerId = 0
      member x.ID with get() = id and set v = id &lt;- v
      member x.CarName with get() = carName and set v = carName &lt;- v
      member x.Dealer with get() = dealer and set v = dealer &lt;- v
      member x.DealerId with get() = dealerId and set v = dealerId &lt;- v
  
  
  type MyContext() =
      inherit DbContext("server=some;database=dbname;user id=uid;password=pwd")
  
      [&lt;DefaultValue&gt;] val mutable dealers : DbSet&lt;Dealer&gt;
      member x.Dealers with get() = x.dealers and set v = x.dealers &lt;- v
  
      [&lt;DefaultValue&gt;] val mutable cars : DbSet&lt;Car&gt;
      member x.Cars with get() = x.cars and set v = x.cars &lt;- v
  
      override x.OnModelCreating(modelBuilder:DbModelBuilder) =
          modelBuilder.Entity&lt;Dealer&gt;()
              .HasMany(ToLinq(&lt;@ fun ka -&gt; ka.Cars @&gt;))
              .WithRequired(ToLinq(&lt;@ fun sk -&gt; sk.Dealer @&gt;))
              .HasForeignKey(ToLinq(&lt;@ fun fg -&gt; fg.DealerId @&gt;)) |&gt; ignore
  
          modelBuilder.Entity&lt;Car&gt;()
              .HasRequired(ToLinq(&lt;@ fun ak -&gt; ak.Dealer @&gt;))
              .WithMany()
              .HasForeignKey(ToLinq(&lt;@ fun ka -&gt; ka.DealerId @&gt;)) |&gt; ignore
  
          modelBuilder.Entity&lt;Dealer&gt;().ToTable("Dealers")
          modelBuilder.Entity&lt;Car&gt;().ToTable("Cars")
  </code></pre>
  
  <p>... The function ToLinq:</p>
  
  <pre><code>let ToLinq (exp : Expr&lt;'a -&gt; 'b&gt;) = 
      let linq = exp.ToLinqExpression() 
      let call = linq :?&gt; MethodCallExpression
      let lambda = call.Arguments.[0] :?&gt; LambdaExpression 
      Expression.Lambda&lt;Func&lt;'a, 'b&gt;&gt;(lambda.Body, lambda.Parameters)
  </code></pre>
  
  <p>... And Program.fs which will get the Dealer and his Cars:</p>
  
  <pre><code>let ctx = new MyContext()
  let joe = ctx.Dealers.Find(1)
  joe.Cars |&gt; Seq.iter(fun x -&gt; printfn "%s" x.CarName)
  printfn "DONE"
  </code></pre>
  
  <p>Any help is appreciated!</p>