【问题标题】:Update statement in linq with external SQL Server 2008 R2 [closed]使用外部 SQL Server 2008 R2 在 linq 中更新语句 [关闭]
【发布时间】:2013-08-09 10:46:26
【问题描述】:

我在通过 linq 更新表时遇到问题。

我为它执行以下代码

    tbl_Customer tblcust= new tbl_Customer();
    tbl_Customer tcust = obj.tbl_Customers.Single(c => c.C_ID == 1);
    tblcust.C_Name = txtcname.Text;
    tblcust.C_Address = txtcaddress.Text;
    tblcust.C_Mobile =Convert.ToInt64( txtcmobile.Text);
    obj.SubmitChanges();

但是这段代码不会影响表中的记录。我使用外部 SQL Server 连接数据库,帮我解决这个问题。

【问题讨论】:

  • 我认为您应该为 tcust 而不是 tblcust 分配值,这是一个新对象

标签: c# sql-server linq sql-update


【解决方案1】:

为什么会这样? tblcust 与 ORM 无关,数据上下文 (obj) 从未听说过 tblcust 引用的对象。你的意思是更新tcust

tcust.C_Name = txtcname.Text;
tcust.C_Address = txtcaddress.Text;
tcust.C_Mobile =Convert.ToInt64( txtcmobile.Text);

tcust 来自 ORM,并且为数据上下文的更改管理器所知,因此它将发现对 tcust 的更改并将它们应用到数据库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多