使用LINQ to SQL来执行更新/设置[英] Using LINQ to SQL to perform an update/set

本文是小编为大家收集整理的关于使用LINQ to SQL来执行更新/设置的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

因此,如果我直接使用此查询或使用 db.ExecuteCommand() ,一切都会正常工作;

update Market..Area set EndDate = NULL where ID = 666 and NID =1 and Code = 36003

但是,我似乎无法在 LINQ to SQL 中执行此操作,我尝试了几种不同的方法,它们似乎都应该工作,这里是一个示例:

var s= db.Area.Single(s => s.ID == 666 && s.Code == 36003 && s.NID == 1);
s.EndDate = null;
db.SubmitChanges();

我不知道还有什么办法可以让它发挥作用.

编辑

我只是想编辑一个项目

推荐答案

Area表上是否定义了主键?Linq 2 SQL 不会对未定义主键的表进行更新.(而且,据我所知,它会默默地失败).

本文地址:https://www.itbaoku.cn/post/1556755.html

问题描述

So, if I use this query directly or by using db.ExecuteCommand() , everything will work fine;

update Market..Area set EndDate = NULL where ID = 666 and NID =1 and Code = 36003

However, I cant seem to do this in LINQ to SQL, I've tried a few different methods that all seem like they should work, here is an example of one:

var s= db.Area.Single(s => s.ID == 666 && s.Code == 36003 && s.NID == 1);
s.EndDate = null;
db.SubmitChanges();

I dont know what else to try to get this working.

EDIT

I am only trying to edit ONE item

推荐答案

Is there a primary key defined on the Area table? Linq 2 SQL will not make an update to a table without a primary key defined. (And, as far as I can remember, it will fail silently).

相关标签/搜索