5 people like it.

F# 3.0 - EntityFramework Type Provider usage with Northwind DB

A small sample how to use F# 3.0 Entity Framework (EF) Type Provider. Visual Studio 11 Beta (and Northwind sample database) needed.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
(*
#r "System.Data.Entity.dll"
#r "FSharp.Data.TypeProviders.dll"
*)
open System
open Microsoft.FSharp.Data.TypeProviders

module Queries =
 
    [<Literal>] 
    let connectionstring = "Server=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;MultipleActiveResultSets=true"
    
    //<interactive at once>
    type private EntityConnection = SqlEntityConnection<ConnectionString=connectionstring, Pluralize = true>
    
    let fetchCustomers = 
        let context = EntityConnection.GetDataContext()

        query { for customer in context.Customers do
                where (customer.Country = "Finland")
                select (customer.ContactName, customer.CompanyName) }
    //</interactive at once>

    let show = fetchCustomers |> Seq.iter (fun i -> Console.WriteLine("Customer: " + fst i + ", Company: " + snd i))

(* Output:

Customer: Pirkko Koskitalo, Company: Wartian Herkku
Customer: Matti Karttunen, Company: Wilman Kala

val show : unit = ()
*)
namespace System
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Data
module Queries

from Script
Multiple items
type LiteralAttribute =
  inherit Attribute
  new : unit -> LiteralAttribute

Full name: Microsoft.FSharp.Core.LiteralAttribute

--------------------
new : unit -> LiteralAttribute
val connectionstring : string

Full name: Script.Queries.connectionstring
type private EntityConnection = obj

Full name: Script.Queries.EntityConnection
val fetchCustomers : Linq.IQueryable<string * string>

Full name: Script.Queries.fetchCustomers
val context : obj
val query : Linq.QueryBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.query
val customer : obj
custom operation: where (bool)

Calls Linq.QueryBuilder.Where
custom operation: select ('Result)

Calls Linq.QueryBuilder.Select
val show : unit

Full name: Script.Queries.show
module Seq

from Microsoft.FSharp.Collections
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
val i : string * string
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
Console.WriteLine() : unit
   (+0 other overloads)
Console.WriteLine(value: string) : unit
   (+0 other overloads)
Console.WriteLine(value: obj) : unit
   (+0 other overloads)
Console.WriteLine(value: uint64) : unit
   (+0 other overloads)
Console.WriteLine(value: int64) : unit
   (+0 other overloads)
Console.WriteLine(value: uint32) : unit
   (+0 other overloads)
Console.WriteLine(value: int) : unit
   (+0 other overloads)
Console.WriteLine(value: float32) : unit
   (+0 other overloads)
Console.WriteLine(value: float) : unit
   (+0 other overloads)
Console.WriteLine(value: decimal) : unit
   (+0 other overloads)
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
Raw view Test code New version

More information

Link:http://fssnip.net/bC
Posted:12 years ago
Author:Tuomas Hietanen
Tags: type provider , ef , entityframework , sql , f# 3.0 , database