使用 LINQ to SQL 更新 [英] Update using LINQ to SQL

查看:23
本文介绍了使用 LINQ to SQL 更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 LINQ to SQL 中根据特定 id 更新记录?

How can I update a record against specific id in LINQ to SQL?

推荐答案

LINQ 是一个查询工具 (Q = Query) - 所以没有神奇的 LINQ 方法来更新单行,除了通过(面向对象)数据上下文(在 LINQ-to-SQL 的情况下).要更新数据,您需要将其取出、更新记录并提交更改:

LINQ is a query tool (Q = Query) - so there is no magic LINQ way to update just the single row, except through the (object-oriented) data-context (in the case of LINQ-to-SQL). To update data, you need to fetch it out, update the record, and submit the changes:

using(var ctx = new FooContext()) {
    var obj = ctx.Bars.Single(x=>x.Id == id);
    obj.SomeProp = 123;
    ctx.SubmitChanges();
}

或者写一个在 TSQL 中执行相同操作的 SP,并通过数据上下文公开该 SP:

Or write an SP that does the same in TSQL, and expose the SP through the data-context:

using(var ctx = new FooContext()) {
    ctx.UpdateBar(id, 123);
}

这篇关于使用 LINQ to SQL 更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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