为什么此 LINQ 查询将值 1 分配给数据库中的 NULL 值?
ExamVersion
类有一个名为 SourceSafeVersionNum
的 int?
属性
当我执行以下代码时:
var query = from examVersion in db.ExamVersions
where examVersion.ExamVersionID == ExamVersionID
select examVersion;
foreach (ExamVersion examVer in query.ToList())
{
yield return examVer;
}
设置了 examVer.SourceSafeVersionNum
为 1
,即使它在数据库中为 NULL
。
当我在 SQL Server 中运行 LINQ 生成的 SQL 代码时,SourceSafeVersionNum
列值为 NULL
(如我所料),但在 foreach 循环中 examVer .SourceSafeVersionNum
为 1
。
我在代码中找不到分配默认值或任何类似逻辑的地方。
有什么想法为什么/在哪里将此值设置为 1
吗?
这是属性声明(由 .dbml 文件生成)
[Column(Storage="_SourceSafeVersionNum", DbType="Int", UpdateCheck=UpdateCheck.Never)]
public System.Nullable<int> SourceSafeVersionNum
{
get
{
return this._SourceSafeVersionNum;
}
set
{
if ((this._SourceSafeVersionNum != value))
{
this.OnSourceSafeVersionNumChanging(value);
this.SendPropertyChanging();
this._SourceSafeVersionNum = value;
this.SendPropertyChanged("SourceSafeVersionNum");
this.OnSourceSafeVersionNumChanged();
}
}
}
The ExamVersion
class has an int?
property named SourceSafeVersionNum
When I execute the following code:
var query = from examVersion in db.ExamVersions
where examVersion.ExamVersionID == ExamVersionID
select examVersion;
foreach (ExamVersion examVer in query.ToList())
{
yield return examVer;
}
examVer.SourceSafeVersionNum
is set to 1
even though it is NULL
in the database.
When I run the SQL code generated by LINQ in SQL Server, the SourceSafeVersionNum
column value is NULL
(as I'd expect) but in the foreach loop the examVer.SourceSafeVersionNum
is 1
.
I can't find anywhere in the code where a default value is assigned or any similar logic.
Any ideas why/where this value is being set to 1
?
Here is the property declaration (generated by a .dbml file)
[Column(Storage="_SourceSafeVersionNum", DbType="Int", UpdateCheck=UpdateCheck.Never)]
public System.Nullable<int> SourceSafeVersionNum
{
get
{
return this._SourceSafeVersionNum;
}
set
{
if ((this._SourceSafeVersionNum != value))
{
this.OnSourceSafeVersionNumChanging(value);
this.SendPropertyChanging();
this._SourceSafeVersionNum = value;
this.SendPropertyChanged("SourceSafeVersionNum");
this.OnSourceSafeVersionNumChanged();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过在属性的
set{}
方法中设置断点,以查看还有哪些内容可能会填充其值?您可能会抓住罪魁祸首,然后查看调用堆栈看看是谁。Have you tried setting a breakpoint in the
set{}
method of the property to see what else might be populating its value? You might catch the culprit in the act, then look at the Call Stack to see who it is.作为后续操作,发生了以下情况:
从数据库检索值的代码被调用两次,但通过两个不同的代码路径。分配值 1 的代码路径被调试器跳过,所以我没有看到它。
As a follow up to this, here is what happened:
The code that retrieved the value from the database was being called twice but through two different code paths. The code path that was assigning the value of 1 was being stepped over by the debugger so I didn't see it.