Search code examples
sql-serverf#type-providers

F# How to use preload child records using Sql Type Provider


I'm using the F# SqlDataConnection data provider to access a database. The code works fine except that it goes to the database for every single nested record. How do I get the query to prefetch some of these records

module Local =
    type Db = SqlDataConnection<"Server=.;Database=CCTFundDb;Trusted_Connection=True">
    let db = 
        let ctx = Db.GetDataContext()
        let dataOptions = Data.Linq.DataLoadOptions()
        dataOptions.LoadWith<Db.ServiceTypes.MemberSubscriptions>(fun p->p.AccountingBooks.AccountingBookEntries |> box)
        ctx.DataContext.LoadOptions <- dataOptions
        ctx
    let _ =  db.DataContext.Log <- Console.Out

If I remove the |> box, it gives the error that it is expecting obj. When I execute in fsx, it complains about the expression.

How do I do this? If it is not possible, is there another type provider that supports loading the data in batches?


Solution

  • As an alternative I can recommend SqlClient Provider. It generates types based on sql you want to execute and the best part is that you don't need to know linq-to-sql(or other ORMs) internals to work with types generated from database.