Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
One To Many Models with Entity Framework
Message
Information générale
Forum:
ASP.NET
Catégorie:
Conception classe
Divers
Thread ID:
01524357
Message ID:
01524363
Vues:
55
>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.

I'm not sure I follow that hierarchy. To me the EF/DataLayer should be the lowest level. If you need a separate Model layer then I would think that would sit above and reference the DataLayer. Personally, I find I can use the EF Entities (with perhaps some added bits in partial classes) as the Model - no need for an additional layer.
>
>The question is this...
> at
>Here is the CustomerModel:
>
>
>public class CustomerModel : _ModelBase
>{
>    public int CustomerId { get; set; }
>    public string CustomerName { get; set; }
>    public List<InvoiceHeaderModel> InvoiceHeaders { get; set; }
>}
>
>
>I have this GetCustomer method on the DataLayer class:
>
>
>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 in the models, but that requires the Models to be coupled to the data layer. If I used eager loading here, how would I get the hierarcchy of models?
>
>What's the right way to do this? .

In geneal I don't think there is a 'right' way. If you know, for example, that you will need the associated Invoice Headers when you load the Customer then eager load using .Include - otherwise lazy load.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform