开发者

Problem with domain services in the Silverlight

I have the following code in the Silverlight RIA WCF services application:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    EmployeesService2 context = new EmployeesService2();
    EntityQuery<Employee> query = context.GetEmployeeQuery();
    context.Load(query);
    int count = context.Employees.Count();
    EmployeeGrid.ItemsSource = context.Employees;
}

It populates Grid with items, but context.Employees.Count() = 0. Why is this so?

Another problem is that I have a similar code for another page against different domain service which based on another entity model and database. But in that case the service didn't return any entities.开发者_StackOverflow What can be possible reason for that? The database is not empty.


This is probably because the context.load is still loading at the moment you are asking for the count of it's items.

try this

var operation = context.Load(query);
operation.Completed += (s,ea) => 
{
    int count = operation.Entities.Count();
    EmployeeGrid.ItemsSource = operation.Entities.ToList();
}


context.Load(query) is an async operation. It returns immediately, before the network request under the hood has retrieved the data. You need to listen for the operation completed event as indicated in Levisaxos' answer.

If you hook up the operation.Completed event and still don't see any data in the context, then it's time to turn your attention to the domain service on the server side. Set a breakpoint in the domain service method corresponding to GetEmployeeQuery (probably GetEmployee) and make sure it's getting called. Then set a breakpoint in the entity collection property in your model - probably in mymodel.Designer.cs, property ObjectSet Employees, to see what data is actually being retrieved.


Ass others have mentioned, it's because Load is asynchronus and returns immediately without any data. You need to respond to the Load operations completed event.

You can do this as @Levisaxos said and handle the Completed event or you can pass in a callback as a parameter to Load. This callback will then be executed when the Load operation has completed.

Hooking up to the event as @Levisaxos said should work too but you should remember to unsubscribe from the event again or you'll leak memory.

http://forums.silverlight.net/forums/p/129624/296266.aspx


One possibility is that you do not have your service set to include the Employee table in the returned data. To do this simple add the [Include] attribute to your metadata file on the Employees property of employee (if this is what is happening).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜