Using LINQ2SQLEB with relations in LINQ 2 SQL transmitted over WCF

Jun 4, 2011 at 12:25 PM

Hello, I would like to share something I've just found. It turns out that when you have a one-to-many relationship, the entities in "relational" properties (let's say Customers and Orders tables where one customer may have many orders). Having sent a Customer entity over WCF, the "customer.Orders" property would not be filled with data normally (you get a NullReferenceException when trying to access it at the client side). To have this EntitySet filled, you need to use its Load() property before transmitting the entity to client. A code example will make it more straightforward:

public Customer GetCustomer(int id) {
var p = (from Customer c in dataContext.Customers where c.UID == id select c).SingleOrDefault();
p.Orders.Load(); // this line loads the EntitySet, you may also 
// disable deferred loading so that this property is filled without this 
// call, but I personally prefer to control which properties are sent so 
// that I control WCF throughput.

return p;

Please correct me if I am wrong.


Jun 5, 2011 at 1:54 PM


Yeah, didn't realize the load method was there for an EntitySet.

Thanks, nice tip!