C# 使用外部SQL Server 2008 R2在linq中更新语句
我在通过linq更新表时遇到问题 我为它执行以下代码C# 使用外部SQL Server 2008 R2在linq中更新语句,c#,sql-server,linq,sql-update,C#,Sql Server,Linq,Sql Update,我在通过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 =Conve
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连接数据库,帮助我解决这些问题 为什么会这样
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
的更改,并将其应用于数据库。我认为您应该为tcust赋值,而不是为新对象tblcust赋值