Reputation: 16236
I have an object User
with a property LastLoggedIn
, following is the code I write to update this field:
using (var o = Conn.OpenDbConnection()) {
var onlyFields = (x => x.LastLoggedIn);
var where = (x => x.Id == item.Id);
return o.UpdateOnly(item, onlyFields, where);
}
I use SQL Server Profiler to capture the output statement, and I got:
UPDATE "Users" SET "LastLoggedIn" = '20131228 11:39:31.441' WHERE ("Id" = "Id")
(Screenshot of Debug + SQL Profiler)
Notice SQL Profiler says "Id" = "Id"? It really should be "Id" = "iNa7EKWjKkKMu...". What have I done wrong? Thanks for your help.
EDIT -------------------------------------------------------
Thank dasblinkenlight I got my answer.
A side note, I do not agree with the way ORMLite library UpdateOnly()
treats item.Id
as a literal "Id", and didn't use its real value.
Meaning this UpdateOnly()
function is only good for hard-coded string unit test. It gets weird in practical use.
Not sure if it is by design or a glitch.
Upvotes: 3
Views: 540
Reputation: 726479
The most likely reason to me is that ORMLite does not view item.Id
as a constant. It sees that it's part of the object being updated, and uses its name, rather than its value.
You should be able to work around this issue by introducing a temporary variable, like this:
using (var o = Conn.OpenDbConnection()) {
var onlyFields = (x => x.LastLoggedIn);
vat itemId = item.Id;
var where = (x => x.Id == itemId);
return o.UpdateOnly(item, onlyFields, where);
}
Now ORMLite would have no visibility into itemId
, so it would have no other choices except using its value as a constant or introducing a parameter for it.
Upvotes: 4