且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我该怎么做才能解决“找不到或未更改行"的问题?SQL Server Compact Edition 数据库上的 LINQ to SQL 中的异常?

更新时间:2023-09-18 18:52:16

这很讨厌,但很简单:

检查 O/R-Designer 中所有字段的数据类型是否与 SQL 表中的数据类型匹配.双重检查是否可以为空!一个列应该在 O/R-Designer 和 SQL 中都可以为空,或者在两者中都不能为空.

Check if the data types for all fields in the O/R-Designer match the data types in your SQL table. Double check for nullable! A column should be either nullable in both the O/R-Designer and SQL, or not nullable in both.

例如,NVARCHAR 列title"在您的数据库中被标记为 NULLable,并且包含值 NULL.即使该列在您的 O/R-Mapping 中被标记为 NOT NULLable,LINQ 也会成功加载它并将 column-String 设置为 null.

For example, a NVARCHAR column "title" is marked as NULLable in your database, and contains the value NULL. Even though the column is marked as NOT NULLable in your O/R-Mapping, LINQ will load it successfully and set the column-String to null.

  • 现在你改变一些东西并调用SubmitChanges().
  • LINQ 将生成一个 SQL 查询包含WHERE [title] IS NULL",以确保标题未被其他人更改.
  • LINQ 查找的属性[title] 在映射中.
  • LINQ 会发现 [title] NOT NULLable.
  • 由于 [title] 不可为空,因此逻辑它永远不可能为 NULL!
  • 所以,优化查询,LINQ用where 0 = 1"替换它,相当于从不"的 SQL.

当字段的数据类型与 SQL 中的数据类型不匹配,或者字段丢失时,也会出现相同的症状,因为 LINQ 将无法确保 SQL 数据在读取数据后没有发生变化.

The same symptom will appear when the data types of a field does not match the data type in SQL, or if fields are missing, since LINQ will not be able to make sure the SQL data has not changed since reading the data.