Natalie Perret
Natalie Perret

Reputation: 8997

How to consume this EF Core C# async method in F#?

I am using F# with Entity Framework and I can't get my head around consuming C# async methods from F#. Despite others SO answers related to similar issues can't really get my head around.

Here is my attempt with the code below, initially synchronous:

let getAirport (id: Guid) =
    use context = new MyContext()
    context.Flights.Find id
    |> (fun x -> if box x = null then None else Some x)

And its async counterpart:

let getAirportAsync (id: Guid) =
    async {
        use context = new MyContext()
        let! flight = context.Airports.FindAsync id |> Async.AwaitTask
        return (fun x -> if box x = null then None else Some x)
    }

However, when both are called in the main:

[<EntryPoint>]
let main argv =
    let myGuid = Guid.NewGuid()
    let airport = {
        Id = myGuid
        Name = "Michelle"
        X = 42.0
        Y = 42.0
    }
    AirportRepository.addAirport airport
    let thisAirport = AirportRepository.getAirport myGuid
    let thisAirportToo = AirportRepository.getAirportAsync myGuid |> Async.RunSynchronously
    assert (thisAirport = Some airport)
    assert (thisAirportToo = Some airport)
    0

It cannot compile:

  Program.fs(61, 13): [FS0001] The type '('a -> 'a option)' does not support the 'equality' constraint because it is a function type
  Program.fs(61, 30): [FS0001] This expression was expected to have type    ''a -> 'a option'    but here has type    ''b option'

I read:

I thought the process to consume an async C# method was:

What am I missing here?

Upvotes: 1

Views: 192

Answers (1)

hvester
hvester

Reputation: 1628

The problem is that in getAirportAsync you discard flight and just return the function. The fix is simple:

let getAirportAsync (id: Guid) =
    async {
        use context = new MyContext()
        let! flight = context.Airports.FindAsync id |> Async.AwaitTask
        return if box flight = null then None else Some flight
    }

Upvotes: 3

Related Questions