Why does this LINQ query assign a value of 1 to a NULL value from the database?
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.
A开发者_运维知识库ny 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();
}
}
}
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.
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.
精彩评论