locked
Lazy Loading via a DAL layer RRS feed

  • Question

  • Really struggling to get the entity framework to work in a way that's suited to my project.  I have a DAL layer that uses the edmx as shown below but I want to lazy load rather than having to use "Include" constantly, I have tried creating the context in a constructor rather than "Using"  but this fails to load the Navigation properties.

        Public Shared Function GetProjects(ByVal isDeleted As Boolean) As List(Of Project)
            Dim projects As New List(Of Project)
            Using context = New SSI_APPSUPPORTEntities
                Dim rows = From s In context.Projects _
                                    .Include("BusContact") _
                                    .Include("TechContact") _
                                    .Include("BusinessCriticality") _
                                    .Include("UnitCriticality") _
                                    .Include("Directorate")
                Where (s.IsDeleted = isDeleted)
                           Select s
                projects = rows.ToList()
            End Using
            Return projects
        End Function

    Wednesday, September 25, 2013 2:59 PM

Answers

  •  

    <copied>

    Dim projects As New List(Of Project)
           
    Using context = New SSI_APPSUPPORTEntities
               
    Dim rows = From s In context.Projects _
                                   
    .Include("BusContact") _
                                   
    .Include("TechContact") _
                                   
    .Include("BusinessCriticality") _
                                   
    .Include("UnitCriticality") _
                                   
    .Include("Directorate")
               
    Where (s.IsDeleted = isDeleted)
                          
    Select s
                projects
    = rows.ToList()
           
    End Using
           
    Return projects

    <end>

    Why do the "Include"?

    If all the relationships are to Project, then you don't have to bring back all the children objects linked to Project.

    If you don't do the "Include", the Project object when created "new" is still going to have null children objects defined in Project. 

    So you can load the children any time you want to load them.

    You would probably to this.

    Project.BusContacts = new List(Of BusContact)

    Project.BusContracts = (from a in db.BusContact where a. ProjectID = heldProjectID select a).ToList

    The above could be used if you are exposing the foreign-key property of ProjectID in BusContact

    If it's not a List(Of T), then you just load a singular of BusContact.

    Project.BusContact = new BusContact

    Project.BusContact = (from a in db.BusContact where BusContactID = heldBusID)

    You can also use the Load function of an EF Entity/Object, but in a DAL situation it  may not be feasible.

    http://msdn.microsoft.com/en-us/data/jj574232.aspx

    So just comeback with the Project object only loaded with its properties and leave the children data behind and load the children as needed at the BLL level.


    • Marked as answer by Fred Bao Wednesday, October 2, 2013 1:20 AM
    Friday, September 27, 2013 4:49 AM
  • Hello,

    With your description, I made a sample and please see it below:

    DAL layer:

    internal List<Order> Execute()
            {
                DataBaseFirstDBEntities db1 = new DataBaseFirstDBEntities();
                List<Order> orderList = (from o in db1.Orders
                                         select o).ToList();
                return orderList;
            }

    View layer:

    List<Order> orderList=test.Execute();
                foreach (Order o in orderList)
                {
                    foreach (OrderDetail od in o.OrderDetails)
                    {
                        Console.WriteLine("OrderDetailCode:" + od.OrderDetailCode);
                    }
                }


    The result:


    The Navigation property is loaded.

    So could you please show the code that fails to load the Navigation properties so that we can help you better.

    Thanks &Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Fred Bao Wednesday, October 2, 2013 1:21 AM
    Thursday, September 26, 2013 3:43 AM

All replies

  • Hello,

    With your description, I made a sample and please see it below:

    DAL layer:

    internal List<Order> Execute()
            {
                DataBaseFirstDBEntities db1 = new DataBaseFirstDBEntities();
                List<Order> orderList = (from o in db1.Orders
                                         select o).ToList();
                return orderList;
            }

    View layer:

    List<Order> orderList=test.Execute();
                foreach (Order o in orderList)
                {
                    foreach (OrderDetail od in o.OrderDetails)
                    {
                        Console.WriteLine("OrderDetailCode:" + od.OrderDetailCode);
                    }
                }


    The result:


    The Navigation property is loaded.

    So could you please show the code that fails to load the Navigation properties so that we can help you better.

    Thanks &Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Fred Bao Wednesday, October 2, 2013 1:21 AM
    Thursday, September 26, 2013 3:43 AM
  •  

    <copied>

    Dim projects As New List(Of Project)
           
    Using context = New SSI_APPSUPPORTEntities
               
    Dim rows = From s In context.Projects _
                                   
    .Include("BusContact") _
                                   
    .Include("TechContact") _
                                   
    .Include("BusinessCriticality") _
                                   
    .Include("UnitCriticality") _
                                   
    .Include("Directorate")
               
    Where (s.IsDeleted = isDeleted)
                          
    Select s
                projects
    = rows.ToList()
           
    End Using
           
    Return projects

    <end>

    Why do the "Include"?

    If all the relationships are to Project, then you don't have to bring back all the children objects linked to Project.

    If you don't do the "Include", the Project object when created "new" is still going to have null children objects defined in Project. 

    So you can load the children any time you want to load them.

    You would probably to this.

    Project.BusContacts = new List(Of BusContact)

    Project.BusContracts = (from a in db.BusContact where a. ProjectID = heldProjectID select a).ToList

    The above could be used if you are exposing the foreign-key property of ProjectID in BusContact

    If it's not a List(Of T), then you just load a singular of BusContact.

    Project.BusContact = new BusContact

    Project.BusContact = (from a in db.BusContact where BusContactID = heldBusID)

    You can also use the Load function of an EF Entity/Object, but in a DAL situation it  may not be feasible.

    http://msdn.microsoft.com/en-us/data/jj574232.aspx

    So just comeback with the Project object only loaded with its properties and leave the children data behind and load the children as needed at the BLL level.


    • Marked as answer by Fred Bao Wednesday, October 2, 2013 1:20 AM
    Friday, September 27, 2013 4:49 AM