LINQ到SQL的SubmitChanges没有更新的领域......为什么呢? [英] Linq-to-Sql SubmitChanges not updating fields ... why?

查看:309
本文介绍了LINQ到SQL的SubmitChanges没有更新的领域......为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我张贴这种<一个href="http://stackoverflow.com/questions/385442/linq-submitchanges-not-updating-certain-fields">question昨天晚上,这使我发现了一个巨大的问题!

I posted this question yesterday evening, which has led me to discover a huge problem!

我在我的数据库名为单位小数列,只要我列的值设置为非零和的SubmitChanges用新值列的更新。如果我尝试的列的值设置为零,所述的SubmitChanges不更新列

I have a decimal column in my database called Units, anytime I set the value of the column to a NON ZERO, and SubmitChanges the column updates with the new value. If I try to set the value of the column to ZERO, the SubmitChanges does not update the column.

data.Units = this.ReadProperty<decimal>(UnitsProperty);
data.UnitPrice = this.ReadProperty<decimal>(UnitPriceProperty);
data.Price = this.ReadProperty<decimal>(PriceProperty);

我已经采取了看看DataContext的日志,我可以看到,与零值的字段不包含在查询中。即使我着急code LINQ的忽略它的变化。

I've taken a look at the DataContext log and I can see that the field with the ZERO value is not included in the query. Even if I try to hard code the change Linq ignores it.

data.Units = 0;
data.UnitPrice = 0;
data.Price = 0;

不用说,这是我的命!任何想法,为什么出现这种情况?

Needless to say this is killing me! Any ideas why this happening?

解决方案

我想通了,我的问题与SO社会的帮助。我的问题正在引起的,当我建立了我的实体附加的事实,列的默认值设为零,所以当它试图将值赋给零... LinqToSql说嘿...什么都没有改变,所以我不更新的价值。

I figured out my problem with the help of the SO community. My problem was being caused by the fact when I created my entity to attach, the default value of the column was set to zero, so when it tried to assign the value to zero ... LinqToSql says hey ... nothing changed, so I am not updating the value.

我在做什么,现在......这只是为了工作如下:

What I am doing now ... just to make it work is the following:

ctx.DataContext.InvoiceItems.Attach(data, true);

这似乎迫使所有的值写入自己的数据库。这适用于现在。

That seems to force all the values to write themselves to the database. This works for now.

推荐答案

我想通了,我的问题与SO社会的帮助。我的问题正在引起的,当我建立了我的实体附加的事实,列的默认值设为零,所以当它试图将值赋给零... LinqToSql说嘿...什么都没有改变,所以我不更新的价值。

I figured out my problem with the help of the SO community. My problem was being caused by the fact when I created my entity to attach, the default value of the column was set to zero, so when it tried to assign the value to zero ... LinqToSql says hey ... nothing changed, so I am not updating the value.

我在做什么,现在......这只是为了工作如下:

What I am doing now ... just to make it work is the following:

ctx.DataContext.InvoiceItems.Attach(data, true);

这似乎迫使所有的值,以自己写入数据库。这适用于现在。

That seems to force all the values to write themselves to the database. This works for now.

这篇关于LINQ到SQL的SubmitChanges没有更新的领域......为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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