How to: Create and Execute Object Queries with Complex Types (Entity Framework)

This example uses the schemas defined in the topic How to: Define a Model with Complex Type (Entity Framework).

To create the project using complex types

  1. Create a console application project named CustomerComplexAddrClient and add references to System.Data.Entity and System.Runtime.Serialization.

  2. Add a reference to the dll built from the project described in the topic How to: Define a Model with Complex Type (Entity Framework).

  3. Add the schemas from the topic How to: Define a Model with Complex Type (Entity Framework) to the same folder as the executable.

  4. Create an Application Configuration file as shown in the example.

  5. Copy the code in the example to the Program.cs file.

  6. Add an application configuration file with the content shown below.

  7. Build and run the project.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="CustomerComplexAddressContext" 
         connectionString="metadata=.;
         provider=System.Data.SqlClient;
         provider connection string=&quot;
         Data Source=serverName;
         Initial Catalog=CustomerWComplexAddr;
         Integrated Security=True;
         multipleactiveresultsets=true&quot;"
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

Example

The example code displays all CCustomers and the inner properties of the CAddress complex type. The object context contains a collection of CCustomers with the complex property Address. The Address property is of type CAddress. All its internal properties are accessible from instances of the CCustomer type.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CustomerComplexAddress;

namespace CustomerComplexAddressClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (CustomerComplexAddressContext objCtx =
                    new CustomerComplexAddressContext())
                {
                    foreach (CCustomer customer in objCtx.CCustomers)
                    {
                        Console.WriteLine("Customer Id: " +
                            "{0} {1}\r\n{2} {3}," +
                            "{4} {5}\n\rPhone: {6}", 
                            customer.CustomerId.ToString(),
                            customer.CompanyName,
                            customer.Address.StreetAddress,
                            customer.Address.City,
                            customer.Address.Region,
                            customer.Address.PostalCode,
                            customer.Address.Phone);
                    }

                    
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

See Also

Community Additions

Show: