How to update a table in database using LINQ in F#?

血红的双手。 提交于 2019-12-13 15:22:16

问题


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

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

回答1:


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




回答2:


LInQ (in any .NET language) is not used for doing insert/update/delete operations. After all, it Language Integrated Query. That having been said, in your example the db value should have methods for passing modified objects back to the database for persistence.



来源:https://stackoverflow.com/questions/2914785/how-to-update-a-table-in-database-using-linq-in-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!