AdventureWorks Sales Connection (EDM)

To use programmable classes built on the Entity Data Model (EDM), a connection must be opened to the data. The connection to the Adventure Works Sales Model is based on a provider connection string and the ObjectContext of the System.Data.EntityClient namespace.

App Config File

Applications that use the EDM must have a configuration file in scope of the executable file. The configuration file includes a connection string identifying the schemas that define the models and mapping, a provider connection string identifying the database management system, and a provider name identifying the System.Data.EntityClient namespace.

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

</configuration>

Application Code

The following code shows how to open a connection to the ObjectContext named AdventureWorksEntities. With the application configuration file in scope the connection can be opened with one line of code: AdventureWorksEntities db = new AdventureWorksEntities().

The ObjectContext provides access to the collections of data used by applications. The following example first opens an ObjectContext and all the instances of the Contact type. Second, it enumerates instances of SalesOrderHeader and uses the navigation property based on an association between SalesOrderHeader and Address to locate and display one line of the address.

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

namespace AdvWrksSalesClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (AdvWksSalesEntities db =
                                     new AdvWksSalesEntities ())
                {
                    foreach (Contact c in db.Contact)
                        Console.WriteLine("{0}, {1}",
                                 c.LastName, c.FirstName);


                    foreach (SalesOrderHeader soHeader in 
                                          db.SalesOrderHeader)
                    {
                        soHeader.Address1Reference.Load();
                    Console.WriteLine(soHeader.Address1.AddressLine1);

                    }

                }
            }
            catch (System.Data.EntityCommandExecutionException e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

See Also

Concepts

AdventureWorks Sales Conceptual Schema (EDM)
AdventureWorks Sales Storage Schema (EDM)
AdventureWorks Sales Mapping Schema (EDM)