LINQ to SQL的提交更改不工作 [英] linq to sql submit changes not working

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

问题描述

我有在WPF客户名称2 colums列表视图和Isvalid.I正在使用LINQ to SQL中摆脱我的SQL table.when我试图更新值表中的数据我没有看到有任何修改表

I have a list view with two colums in wpf Customername and Isvalid.I am using linq to sql to get the data from my sql table.when i am trying to update a value to the table i dont see any changes to the table.

下面是我的代码,当我保存按钮点击:

Here is my code when i click on the save button:

 try
        {
            CustomersDataContext dataContext = new CustomersDataContext();
            Customer customerRow = MyDataGrid.SelectedItem as Customer;
            string m = customerRow.CustomerName;
            Customer customer = (from p in dataContext.Customers
                                 where p.CustomerName == customerRow.CustomerName
                                 select p).Single();

            customer.Isvalid=false;

            dataContext.SubmitChanges();
            MessageBox.Show("Row Updated Successfully.");

        }
        catch (Exception Ex)
        {
            MessageBox.Show(Ex.Message);
            return;
        }



我可以看到我能够根据所选择的客户名称来查询记录但价值不更新。

I can see that i am able to query the record based on the customername selected but the value is not updating.

我会很高兴,如果有人能指出我在哪里丢失更新的IsValid值与数据库的逻辑。

I would be glad if some one can point out where am i missing the logic to update the "ISVALID" value to the data base.

推荐答案

首先,这里是你的使用(get_datacontext){...} 块?您需要处置的DataContext 的S当你与他们做的!

Firstly, where's your using(get_datacontext){...} block? You need to dispose of DataContexts when you are done with them!

反正...

我的猜测是,更新语句生成where子句这是过于紧张,或者是完全错误的。

My guess is that the update statement is generating a where clause that's far too tight, or just plain wrong.

我会是在LINQ to SQL设计检查每个列的检查更新属性在你的映射表。做最简单的事情是主键列设置为总是键,将所有其他人从不。您也可以考虑将它们设置为 WhenChanged

I would be checking the 'Update Check' property of each of the columns in your mapped table in the Linq to Sql designer. The simplest thing to do is to set the primary key column to Always and set all the others to Never. You can also consider setting them to WhenChanged.

设计师的默认行为一般是将其设置为总是的一切;不仅这会引起可怕的更新WHERE 条款,但偶尔也会产生问题。显然,这种行为需要进行适当的并发检查(即两个线程更新同一行); 。所以要意识到这一点。

The designer's default behaviour is generally to set it to Always for everything; not only does this cause horrible WHERE clauses for updates, but can occasionally also cause problems. Obviously such behaviour is required for proper concurrency checking (i.e. two threads updating the same row); so be aware of that.

呵呵,别的想法 - 你也可以得到这种行为,如果你的表没有在设计师指定一个主键 - 让当的SubmitChanges 被称为正在生成确定的一列是

Oh, thinking of something else - you can also get this behaviour if your table doesn't have a primary key designated in the designer - make sure one of the columns is.

最后,您可以检查SQL。由TextWriter的连接到 DataContext.Log 属性;还是在VS2010同样的IntelliTrace将收集,如果你开始调试正在运行的所有ADO.Net查询。这是为什么调试预期L2S东西是不工作非常宝贵的。

Finally you can check the SQL being generated when SubmitChanges is called; by attaching a TextWriter to the DataContext.Log property; or equally IntelliTrace in VS2010 will collect all ADO.Net queries that are run if you start with debugging. This is invaluable in debugging why L2S stuff isn't working as expected.

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

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