如何在F#中使用LINQ更新数据库中的表? [英] How to update a table in database using LINQ in F#?

查看:48
本文介绍了如何在F#中使用LINQ更新数据库中的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了很多有关如何查询数据库的示例,但是没有有关如何更新记录的示例.下面是我编写的用于检索表的简单代码,但是有人可以向我解释如何修改字段(例如lastActiveDate)并更新数据库中的表

I have seen plenty of examples on how to query the database but nothing on how to update records. Below is the simple code that I wrote to retrieve a table, but can someone explain me how can I modify a field, say lastActiveDate, and update the table on the database

谢谢你, suday

Thank you, suday

open System
open Microsoft.FSharp.Linq

let connString = "Server=localhost;Database=myDb;Trusted_Connection=True;"
let db = new MyDb(connString)
db.Log <- System.Console.Out

let res =
    Query.query <@ seq {
        for users in db.userAccounts do
        yield users
     } @>
     |> List.ofSeq

printfn "Totla users: %d" res.Length

推荐答案

由于MyDB是System.Data.Linq.DataContext,因此它将跟踪它加载的每个对象.只需从MyDB获取实例,在该实例上设置属性的值,然后调用MyDB.SubmitChanges

Since MyDB is a System.Data.Linq.DataContext, it tracks each object that it loads. Simply acquire an instance from MyDB, set a property's value on that instance, and call MyDB.SubmitChanges

这篇关于如何在F#中使用LINQ更新数据库中的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆