LINQ到SQL - 未能更新 [英] Linq to SQL - Failing to update

查看:130
本文介绍了LINQ到SQL - 未能更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在遇到一些麻烦与更新LINQ to SQL的实体。
出于某种原因,我可以更新我的项目实体的每一个领域之外的名称

I'm having some troubles with updating linq to sql entities. For some reason, I can update every single field of my item entity besides name.

下面两个简单的测试中,我写道:

Here are two simple tests I wrote:

 [TestMethod]
        public void TestUpdateName( ) {
            using ( var context = new SimoneDataContext( ) ) {
                Item item = context.Items.First( );

                if ( item != null ) {
                    item.Name = "My New Name";
                    context.SubmitChanges( );
                }
            }
        }

        [TestMethod]
        public void TestUpdateMPN( ) {
            using ( var context = new SimoneDataContext( ) ) {
                Item item = context.Items.First( );

                if ( item != null ) {
                    item.MPN = "My New MPN";
                    context.SubmitChanges( );
                }
            }
        }



不幸的是,TestUpdateName()失败并出现以下错误:
System.Data.SqlClient.SqlException:关键字WHERE..

和这里的输出SQL:

UPDATE [DBO] [项目] SET WHERE([ID]
= @ P0)AND([类别ID] = @ P1)和([MPN] = @ P2)AND([高度] = @ P3)和
([宽度] = @ P4)AND([重量] = @ P5)
和([长] = @ P6)和
([AdministrativeCost] = @ P7)
- @ P0:输入INT(大小= 0; PREC = 0;比例尺= 0)[1]
- @ P1:输入int(尺寸= 0; PREC = 0;比例尺= 0)[1]
- @ P2:输入VARCHAR(尺寸= 10; PREC = 0;规模= 0)[我的新MPN]
- @ P3:输入十进制(大小= 0; PREC = 5;规模= 3)[30.000]
- @ P4:输入小数(尺寸= 0; PREC = 5;规模= 3)[10.000]
- @ P5:输入十进制(大小= 0; PREC = 5;规模= 3)[40.000]
- @ P6:输入十进制(大小= 0; PREC = 5;规模= 3)[30.000]
- @ P7:输入货币(大小= 0; PREC = 19;规模= 4)[350.0000]
- 上下文:sqlProvider的(SQL2008)型号:AttributedMetaModel构建:
3.5.30729.4926

UPDATE [dbo].[Items] SET WHERE ([Id] = @p0) AND ([CategoryId] = @p1) AND ([MPN] = @p2) AND ([Height] = @p3) AND ([Width] = @p4) AND ([Weight] = @p5) AND ([Length] = @p6) AND ([AdministrativeCost] = @p7) -- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1] -- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [1] -- @p2: Input VarChar (Size = 10; Prec = 0; Scale = 0) [My New MPN] -- @p3: Input Decimal (Size = 0; Prec = 5; Scale = 3) [30.000] -- @p4: Input Decimal (Size = 0; Prec = 5; Scale = 3) [10.000] -- @p5: Input Decimal (Size = 0; Prec = 5; Scale = 3) [40.000] -- @p6: Input Decimal (Size = 0; Prec = 5; Scale = 3) [30.000] -- @p7: Input Money (Size = 0; Prec = 19; Scale = 4) [350.0000] -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4926

正如你所看到的,正在生成无更新(SET是空的...)
口不知道为什么会这样。

As you can see, no update is being generated (SET is empty ...) I have no clue why is this happening.

和事先已经......是的,表项目有一个PK(ID)。 !
谢谢的提前

And already in advance ... YES, the table Item has a PK (Id). Thank you in advance!

更新:
看来,错误的是造成重写GetHashCode()方法。
这是我目前实施:

Update: It appears that the error is caused by overriding GetHashcode(). This is my current implementation:


返回的String.Format({0} | {1} | { 2} | {3},名称,标识,UPC,AdministrativeCost).GetHashCode();

推荐答案

这听起来像你的DBML可能是不同步的。您应该删除该表并重新添加它们并再次尝试运行它。

It sounds like your DBML might be out of synch. You should delete the tables and re-add them and try and run it again.

只要删除你的产品表。手动和重新添加

Just delete your Items table manually and re-add it.

修改:根据您的编辑,你应该检查出以下螺纹的关于 GetHashCode的

EDIT: Based on your edit, you should check out the following thread regarding GetHashCode.

http://social.msdn.microsoft.com/forums/en-US/linqtosql/thread/6cc6c226-f718-4b22-baad-dba709afe74b/

净规则宣称的GetHashCode()
和equals()必须始终是一前一后实现
。两个对象
中等于必须具有相同的哈希
码。

.Net rules claim that GetHashCode() and Equals() must always be implemented in tandem. Two objects that are equal must have the same hash code.

此外,GetHashCode的组合()
+的equals()形成身份的实体的概念。如果你把它根据
字段值(比其他PK),那么
身份的变化,你改变
领域。这是坏的,如果L2S必须在词典的基础上
实体的身份,尤其是
查找
等信息,如果L2S需要找到在
身份缓存的实体!

Also, the combination of GetHashCode() + Equals() forms the entity's concept of identity. If you make it based on field values (other than PK) then the identity changes as you change the fields. This is bad if L2S must lookup other info in a dictionary based on the entity's identity, and especially if L2S needs to find an entity in its identity cache!

建议:不改变$ b $的身份b中的一个实体。 L2S希望它是在对象的自然(地址
型)基于身份的

Advice: don't change the identity of an entity. L2S expects it to be based on the object's natural (address based) identity.

这篇关于LINQ到SQL - 未能更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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