I'm trying to connect F# with an existing Postgresql database in a way that lets me take advantage of the database types during development (via Type Providers or Entity Framework, or a combination thereof).
The raw connection is straightforward to do using Npgsql. For example, the following code segment prints the contents of a Postgres table using F#.
open Npgsql
let connectionString = "(my postgres connection string)"
let getResultSet (connection:NpgsqlConnection) (queryString:string) =
let command = new NpgsqlCommand(queryString, connection)
let dataReader = command.ExecuteReader()
seq {
while dataReader.Read() do
yield [for i in [0..dataReader.FieldCount-1] -> dataReader.[i]]
}
let printResults =
let conn = new NpgsqlConnection(connectionString)
conn.Open()
try
let resultSet = getResultSet conn "select * from myTable;"
for r in resultSet do
for d in r do
printf "%s\t" (d.ToString())
printfn ""
finally
conn.Close()
What's the best way to get a Type Provider for this set up or enable using Entity Framework with Postgresql on F#?
Since Npgsql is a ADO.NET provider you should be able to use the SqlEntityConnection
type provider with an entity framework DBML file.
For more information see: http://msdn.microsoft.com/en-us/library/hh362322.aspx.