Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the SQL generated by the fsharp.data.sqlprovider type provider

Tags:

sql-server

f#

I'm learning F# and playing around with type providers and I've connected the awesome FSharp.Data.SQLProvider to a SQL Server instance. I'm able to use F#'s query expression syntax to perform queries against the database but I'd like to see the SQL that is generated by the type provider. I've tried to assign Console.Out to what I assume is the DataContext but I get an error saying

Error FS0810: Property 'Log' cannot be set

How do I log the SQL generated by the type provider?

#r @"packages/SQLProvider/lib/FSharp.Data.SqlProvider.dll"
#r @"System.Data.Linq.dll"

open System
open System.Data.Linq
open FSharp.Data.Sql

[<Literal>]
let connectionString = @"SuperSecretString"

type Sql = SqlDataProvider<
            ConnectionString = connectionString,
            DatabaseVendor = Common.DatabaseProviderTypes.MSSQLSERVER,
            IndividualsAmount = 1000>

let ctx = Sql.GetDataContext()

ctx.Log <- Console.Out  // error FS0810: Property 'Log' cannot be set
like image 641
ClaySchick Avatar asked Aug 26 '16 20:08

ClaySchick


1 Answers

You can listen to the event SqlQueryEvent:

FSharp.Data.Sql.Common.QueryEvents.SqlQueryEvent |> Event.add (printfn "Executing SQL: %O")
like image 149
Tuomas Hietanen Avatar answered Sep 18 '22 14:09

Tuomas Hietanen