How to Update Multiple Fields from an Object with FSharp.Data.SqlClient

184 Views Asked by At

I've been looking into F# in relation to CRUD with data . and I have been using FSharp.Data.SqlClient

I can't see an easy way to update all fields from an object.

For example I want to execute the code below - and then I want to present that data to a web page. The web page will make some changes ( maybe ) - then I want to use the object to update the database.

I don't want to have to write code to map every single field. Additionally, I am still trying to get my head around how this is done in an immutable way.

let getOrders2() = 
    let cmd = new SqlCommandProvider<"
    SELECT top 10 * from orders
    " , connectionString>(connectionString)

    let orders = cmd.Execute()

    let o2 = orders |> Seq.toList
1

There are 1 best solutions below

4
On

Updating multiple fields at once from an object is straightforward:

let updateEmployee employee =
    use cmd =
        new SqlCommandProvider<
            "update Employees set \
            FirstName = @FirstName,
            LastName = @LastName
            where EmployeeID = @EmployeeID",
            connectionString>(connectionString)
    let nRows =
        cmd.Execute(
            employee.FirstName,
            employee.LastName,
            employee.EmployeeID)
    assert(nRows = 1)

Writing a web app with immutable objects is a big topic. Are you using F# just on the server, or also in the client?