Linq to SQL - 无法更新

时间:2023-02-16
本文介绍了Linq to SQL - 无法更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 linq 更新为 sql 实体时遇到了一些麻烦.出于某种原因,除了 name 之外,我可以更新我的 item 实体的每个字段.

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"附近的语法不正确..

Unfortunately, TestUpdateName() fails with the following error: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'WHERE'..

这是输出的 SQL:

UPDATE [dbo].[Items] SET WHERE ([Id]= @p0) AND ([CategoryId] = @p1) AND ([MPN] = @p2) AND ([Height] = @p3) AND([宽度] = @p4) AND ([重量] = @p5)AND ([长度] = @p6) AND([管理成本] = @p7)-- @p0: 输入整数 (大小 = 0; Prec = 0; 比例 = 0) [1]-- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [1]-- @p2: 输入 VarChar (Size = 10; Prec = 0; Scale = 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.

并且已经提前......是的,表Item有一个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:

<代码>return string.Format( "{0}|{1}|{2}|{3}", Name, Id, 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.

只需手动删除您的 Items 表并重新添加即可.

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

EDIT:根据您的编辑,您应该查看以下有关 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/

.Net 规则声称 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!

忠告:不要改变身份一个实体.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 to SQL - 无法更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:在 Visual Studio 中查看 LINQ 执行的 SQL 的方法? 下一篇:如何在运行时更改 LINQ O/R-M 表名/源?

相关文章

最新文章