Linq to SQL - Failing to update(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 - 无法更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Linq to SQL - 无法更新
基础教程推荐
- 当键值未知时反序列化 JSON 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 创建属性设置器委托 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01