Reputation: 107
I have a WCF layer to a quite complex database that i read using LINQ to Entities. Recently though, a weird error has emerged. I get null references when executing the queries, but only sometimes (the data in the database is NOT changing). I have two general cases described below, and it's EITHER the one OR the other failing. If i restart the site accessing the service so that the connection is reinitialized, the roles switch. Now the other case is failing, while the first works. So.. I'm quite confused. Does anybody come to think of anything?
Case 1:
string code = "xxx";
int version = 1;
Language lang = Language.en;
var languages = this.uc.MainTable
.Expand(a => a.Program.Area)
.Expand(a => a.Plang)
.Expand(a => a.Lang)
.Where(a =>
a.Program.Code.Equals(code, StringComparison.OrdinalIgnoreCase)
&& a.Lang != null
&& a.Program.Version == version)
.ToList();
var language = languages.Where(a => a.Lang.LangID == (int)lang).SingleOrDefault();
In the case above, when enumerating language
, a.Lang
is null, and the method fails. Again, this is only sometimes. With the exact same input parameters and database content, the query works again after reinitializing the service connection. The second case seems to fail when the first one successes and vice versa. The other case is this:
Case 2:
Language lang = Language.en;
var programs = this.uc.SomeTable
.Expand(a => a.Program)
.Expand(a => a.Plang)
.Expand(a => a.Program.Level)
.Where(a =>
a.Program.Version == 1
&& a.LangID == (int)lang
&& a.Program.Occasion.Any(b => b.StatusID != 3));
programs = programs.Where(a => a.Program.Occasion.Any(b => b.Date == "2010-01-01"));
When programs
is enumerated, a.Program.Occasion
in the last line is null and causes a null reference. Again, this is intermittent, despite identical database and parameters.
I'm grateful for even the slightest idea of what might be causing this...
Upvotes: 1
Views: 310
Reputation: 13320
Answered here: http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataservices/thread/9fb8648a-fb4f-4ebd-ad5a-c911cd00a812.
This is usually caused by DataServiceContext.MergeOption. The default setting means that client will never update the entity after the first query. Which means that additional queries will not populate navigation properties even if they were expanded in the query. The fix is to set MergeOption to OverwriteChanges.
Upvotes: 1