New Posts New Posts RSS Feed: EntityServerException with children reference
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

EntityServerException with children reference

 Post Reply Post Reply
Author
philpastor View Drop Down
Newbie
Newbie
Avatar

Joined: 09-Apr-2008
Location: United States
Posts: 9
Post Options Post Options   Quote philpastor Quote  Post ReplyReply Direct Link To This Post Topic: EntityServerException with children reference
    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
 
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
philpastor View Drop Down
Newbie
Newbie
Avatar

Joined: 09-Apr-2008
Location: United States
Posts: 9
Post Options Post Options   Quote philpastor Quote  Post ReplyReply Direct Link To This Post 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));
}

Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
philpastor View Drop Down
Newbie
Newbie
Avatar

Joined: 09-Apr-2008
Location: United States
Posts: 9
Post Options Post Options   Quote philpastor Quote  Post ReplyReply Direct Link To This Post Posted: 01-May-2008 at 5:42pm
 
Awesome. Thanks, Kim. When are those bits going to be released? :)


Edited by philpastor - 01-May-2008 at 5:44pm
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post 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).
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down