Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have Customers, Invoice Headers, Invoice Details, and Products. All are one-to-many relationships.

My solution has a data layer project built using entity framework. I also have a models project with models for each entity. The data layer project references the models project.

So far, pretty typical.

The question is this...

Here is the CustomerModel:
public class CustomerModel : _ModelBase
{
    public int CustomerId { get; set; }
    public string CustomerName { get; set; }
    public List<invoiceheadermodel> InvoiceHeaders { get; set; }
}
</invoiceheadermodel>



I have this GetCustomer method on the DataLayer class:
C#
public static CustomerModel GeCustomer(int CustomerId)
{
    CustomerModel retVal = (from c in context.tblCustomers
                            where c.CustomerId == CustomerId
                            select new CustomerModel
                            {
                                CustomerId = c.CustomerId,
                                CustomerName = c.CustomerName
                            }).FirstOrDefault();
    return retVal;
}


The problem is in this method I did not load the InvoiceHeaders property. If I did that here, would you also then load the invoice details, and then the products?

What if all I wanted was a simple list of Customers so that I could populate a simple list?

I could use lazy loading, but that requires the Models to be coupled to the data layer.

What's the right way to do this?
Posted
Updated 22-Sep-11 6:35am
v4

1 solution

You can add a params array that references each linked entity you want to eager load to the method signature in your data layer:

C#
public static CustomerModel GetCustomer(int CustomerId, params string[] eagerLoadCollections)
{
    var retVal = (from c in context.tblCustomers
                            where c.CustomerId == CustomerId
                            select new CustomerModel
                            {
                                CustomerId = c.CustomerId,
                                CustomerName = c.CustomerName
                            });

    foreach(string eagerLoadCollection in eagerLoadCollections)
    {
       retVal = retVal.Include(eagerLoadCollection);
    }

    return retVal.FirstOrDefault();
}


This enables you to load as many related entity collections as you want:

C#
var customer = DataLayer.GetCustomer(123);


or:

C#
var customer = DataLayer.GetCustomer(123, "InvoiceHeaders", "OtherRelatedEntities", "etc.");


Whether it's the right way I can't say. It works for me :)
 
Share this answer
 
v2
Comments
Kevin Marois 22-Sep-11 14:28pm    
That's an interesting idea, but it doesn't give you back a collection of models.
jim lahey 22-Sep-11 15:18pm    
did I not understand the question correctly?
Kevin Marois 22-Sep-11 16:08pm    
Maybe not. I'm trying to load a hierarchal list of model objects from the data.
Kevin Marois 22-Sep-11 17:57pm    
At one point I was using Linq To SQL in my DL. I was able to do this

CustomerModel retVal = (from c in context.tblCustomers where c.CustomerId == CustomerId select new CustomerModel { CustomerId = c.CustomerId, CustomerName = c.CustomerName, InvoiceHeader = getInvoicesForCustomer(c.CustomerId) }).FirstOrDefault();

Now with EF I can't do it.

There HAS to be a way to do this!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900