简体   繁体   English

如何使用 FSharp.Data.SqlClient 从 Object 更新多个字段

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

I've been looking into F# in relation to CRUD with data.我一直在研究与数据 CRUD 相关的 F#。 and I have been using FSharp.Data.SqlClient我一直在使用 FSharp.Data.SqlClient

I can't see an easy way to update all fields from an object.我看不到从 object 更新所有字段的简单方法。

For example I want to execute the code below - and then I want to present that data to a web page.例如,我想执行下面的代码 - 然后我想将该数据呈现给 web 页面。 The web page will make some changes ( maybe ) - then I want to use the object to update the database. web 页面将进行一些更改(也许) - 然后我想使用 object 来更新数据库。

I don't want to have to write code to map every single field.我不想在每个字段中都为 map 编写代码。 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

Updating multiple fields at once from an object is straightforward:从 object 一次更新多个字段非常简单:

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.使用不可变对象编写 web 应用程序是一个很大的话题。 Are you using F# just on the server, or also in the client?您是在服务器上还是在客户端上使用 F#?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM