0

i have a school class that have a list of rooms

public class School
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual IList<Room> Rooms { get; set; }
}

public class Room
{
    public int Id { get; set; }
    public int SchoolId { get; set; }
    public string Name { get; set; }
}

now i wanna to get list of schools with rooms of them using stored procedure.

I know it's possible and so simple with 'Include' function in code first mode.

it's possible in NHibernate and MyBatis.Net Orms.

is it possible In EF ?

Ali Jalali
  • 145
  • 1
  • 3
  • 10
  • Possible duplicate. https://stackoverflow.com/questions/26468371/can-i-lazy-load-a-navigation-property-by-delegating-to-a-stored-procedure-in-ef – William Xifaras Jul 18 '16 at 16:22

1 Answers1

0

You can use SP with DbFunction to get a lazy results:

[DbFunction("MyContext", "GetSchools")]
public IQueryable<School> GetSchools()
{
  ...

Now you can just working with the query and to materialize IQueryable you have to call ToList(), First(), etc. This command will be send the database.

You can do the same for the Room class.

Here is an example:

https://codefirstfunctions.codeplex.com/

Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70