How to return an object that is a query for multiple tables

I have a web service that uses the Entity Framework for storage and provides a public API for CRUD operations.

If I have an object, such as a User, which has a 1 to many relationship with the Car object, how can I easily return a user instance that looks like this to my GetUser web service method (int userId):

public class User{
  string Id;
  IEnumberable<Car> Cars;
}

Does this work by default within an entity because I assume that the Cars property is lazy when used on the server side.

+5
source share
3 answers

Entity objects are serializable, and you automatically get the Cars property.

LINQ Cars . Cars -, . Cars.

1) Include ( "" ) LINQ.

2) .

3) Cars User. ,

userA.Cars.Load()
+2

, [Include] User. , , getUsers() . - .

public IQueryable<User> GetUsers()
{
    return this.ObjectContext.User.Include("Cars");
}

, , -.

!

+2

I used LINQ-To-SQL to achieve a similar result, and for me it just worked. I will try and see what happens. Perhaps you need to explicitly define [DataContract]for elements.

0
source

All Articles