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

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

问题描述

如何针对LINQ中的特定ID将记录更新为SQL?

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更新到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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