AdventureWorks Sales Connection (EDM)

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

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" ?>
    <add name=" AdvWksSalesEntities" 
         provider connection string=
         &quot;Data Source=serverName;
         Initial Catalog=AdventureWorks;
         Integrated Security=True;
         providerName="System.Data.EntityClient" />


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)
                using (AdvWksSalesEntities db =
                                     new AdvWksSalesEntities ())
                    foreach (Contact c in db.Contact)
                        Console.WriteLine("{0}, {1}",
                                 c.LastName, c.FirstName);

                    foreach (SalesOrderHeader soHeader in 


            catch (System.Data.EntityCommandExecutionException e)

See Also

Community Additions