Search code examples
c#entity-frameworkadventureworks

Mapping entity properties to other entities


Inside the AdventureWorks database we have Individuals, Contacts and Customers. These 3 tables are related. My goal is to get the FirstName, LastName and Email up on the Customers when I call them.

Is there a way to resolve this with mappings?

Thanks in advance. Kyor

EDIT: Structure: Structure of the table


Solution

  • I can think of two ways:

    1) Using Include()

     var customers = context.Customer.Include("Individual.Contact");
    

    Then you can access the properties by: customers.First().Individual.Contact.FirstName;

    2) Projecting to a new Type

      var customers = from c in context.Customer
                      select new NewCustomerType
                      {
                        Customer = c,
                        FirstName = c.Individual.Contact.FirstName,
                        LastName = .Individual.Contact.LastName,
                        .
                        .
                        .
                      };