fsprojects / SQLProvider

A general F# SQL database erasing type provider, supporting LINQ queries, schema exploration, individuals, CRUD operations and much more besides.
https://fsprojects.github.io/SQLProvider
Other
570 stars 144 forks source link

Cannot get updated rows after insertion of a new row #657

Closed dmitryrusak closed 4 years ago

dmitryrusak commented 4 years ago

Description

Hi, i have tried to use SQLProvider to communicate with our MS SQL database. I have implemented two methods - one for getting all rows from the table and another one for creation (insertion) of a new row to a table. After that i have created a unit test which idea is to GetAll rows number , insert a new row and GetAll rows number again and check if its incremented by one. See the code below:

Repro steps

module EDM =
    type sql = SqlDataProvider<Common.DatabaseProviderTypes.MSSQLSERVER
        ,ConnectionString=Global.ConnectionString,UseOptionTypes=true>

    module Event =
        (* Create a new Event entry *)
        let Log (eventType:int) (message:string)  =
            let db = sql.GetDataContext()
            let row = db.Dbo.Event.Create()
            try 
                row.EventType <- eventType
                row.Message <- Some(message)
                db.SubmitUpdates()
            with
                | exn -> raise exn 

        (* get all event entries *)
        let  GetAll =
             let db = sql.GetDataContext()
             db.GetUpdates() |> ignore
             query {
                for e in db.Dbo.Event do
                select (Some e)
             } |> Seq.toList
(* Here is a unit test  *)
[<TestMethod>]
    member this.LogTest() =
        try 
            let a = List.length EDM.Event.GetAll 
            EDM.Event.Log Global.Event.EventType.Info "This is a test message"  |> ignore
            let b = List.length EDM.Event.GetAll
            Assert.IsTrue(b - a = 1)
        with
            | _ -> Assert.Fail()

Expected behavior

I expected that b should be a+1 because the new row added.

Actual behavior

Wrong behaviour is in my test the b is the same is a, so the db context does not see the added row. (i see that the row added in the database)

Known workarounds

Please provide a description of any known workarounds.

Related information

Please explain what am i doing wrong

Thorium commented 4 years ago

The GetAll should be a function, not a property. So you want to say GetAll() with the brackets.

As far as I know, you don't have to do GetUpdates(), the idea is not refresh, but just to ask "in this context, what is not yet submitted to database", which is nothing.

dmitryrusak commented 4 years ago

Thanks for help, works now!