Print Page | Close Window

EntityServerException with children reference

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=794
Printed Date: 21-Jan-2026 at 12:14pm


Topic: EntityServerException with children reference
Posted By: philpastor
Subject: EntityServerException with children reference
Date Posted: 30-Apr-2008 at 10:15am
 
I am receiving an "EntityServerException: Sequence contains no elements" when I try to navigate to an association property that does not have any children.
 
I have a Company object that contains a collection of Employees. I expected that if there were no employees I would receive an empty List<Employee> collection. But instead I received the exception. What is the correct way to determine whether or not this collection is empty?
 
Just as a note, if I create an employee in the database, then the Employee is returned in the association (so the model is wired up correctly).
 
Phil
 



Replies:
Posted By: kimj
Date Posted: 01-May-2008 at 8:27am

How are you accessing the child collection?  Something simple like:

   var list = Company.Employees;
 
or more complex:
 
   var list = Company.Employees.Skip(2).Take(2);
 
 
These should actually work, but we need a little more information to help uncover the problem.   Are you able to recreate the problem using any of the tutorials?


Posted By: philpastor
Date Posted: 01-May-2008 at 11:15am
Kim,
 
Thank you for the response.
 
This may be my lack of understanding of LINQ, but using the tutorial and the Northwind database I executed the following commands:
 
aManager = new DomainModel.Manager();
DomainModel.Customer aCustomer = aManager.Customers.Where(c => c.CustomerID == "PARIS").First();
System.Diagnostics.Debug.WriteLine(string.Format("Customer Id: {0} - Number of Orders: {1}", aCustomer.CustomerID, aCustomer.Orders.Count));

This results in the exception. In this example the customer "PARIS" has no orders. Does the First() method sever the ability to lazy load? 

If I change it to the following, then it works just fine:
aManager = new DomainModel.Manager();
var query = aManager.Customers.Where(c => c.City.Length == 5);
foreach (DomainModel.Customer aCustomer in query)
{
Console.WriteLine(string.Format("Customer Id: {0} - Number of Orders: {1}",aCustomer.CustomerID, aCustomer.Orders.Count));
}



Posted By: kimj
Date Posted: 01-May-2008 at 2:26pm
Oh, you're not doing anything wrong, it looks like we do have a bug.
 
What's happening is that when the first query executed for a given type (like Customer) is one which forces immediate execution, i.e., a query using First, Max, Count, etc., then we're not correctly doing some initialization required to handle related entities.  When we attempt to retrieve the related entities and none are returned we get the unhandled sequence exception you see.  You've found a unique case, since if you'd first queried for a Customer using deferred execution the error wouldn't have occurred; also if you'd done a First() query on a customer having orders the problem wouldn't have surfaced.
 
The good news - due to some refactoring we've already done in query processing -  is that this problem has already been fixed in our latest bits. 
 
For your purposes right now, I guess the workaround could be to try to avoid using "immediate execution" type of queries.  In this case, a ToList() followed by a First(), like this:
 
    aManager.Customers.Where(c => c.CustomerID == "PARIS").ToList().First();
 
would avoid the problem.
 
Thanks for bringing this to our attention.


Posted By: philpastor
Date Posted: 01-May-2008 at 5:42pm
 
Awesome. Thanks, Kim. When are those bits going to be released? :)


Posted By: kimj
Date Posted: 01-May-2008 at 6:51pm
Well ... we do keep pushing the date for Beta2 off, don't we? Smile 
 
Right now, we're looking at mid-May (yes, 2-3 weeks from now).



Print Page | Close Window