Using the Northwind Object Model (EDM)

To use the Northwind object model designed and built in the neighboring topics in application code, add a reference to the DLL and a using directive specifying the NorthwindLib namespace. The types of the namespace will be usable in application code without SQL syntax.

Configuration File and Connection String

Using the object model requires a connection to the database where application data is stored. An entity connection to the runtime objects provided by the DLL built from the schemas is also needed.

The exe.config file contains a connection string that is used to connect to a SQL Server database and establish an entity connection. By using an entity connection, the entities and associations in the object model can be accessed from code.

The connection string text must be added to the exe.config file by the developer. This application specifies the class Northwind. The assignment providerName="System.Data.EntityClient" specifies an entity connection that uses the mapping schema defined in the Northwind Mapping Specification.

The connection string also identifies the server that is used by the SQL Connection: provider connection string="server=servername".

The following example shows the contents of the exe.config file.

?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="Northwind" 
                 connectionString='Metadata=.;
                 Provider=System.Data.SqlClient;
                 Provider Connection String="server=servername;
                 database=Northwind;Integrated Security=true;
                 Connection Timeout=5;multipleactiveresultsets=true"'
                 providerName="System.Data.EntityClient"/>
    </connectionStrings>
</configuration>

The path to the metadata, which includes the conceptual schema, the storage metadata, and the mapping specification, is indicated by the assignment Metadata=.. In this example, the files are in the same folder as the executable, therefore the path is specified by the period (.) as in the syntax Metadata=.. If the schemas and mapping files are not in the same folder, you must specify the full path.

Note

This connection string sets multiple active result sets to true as required to invoke the Load method on associations when another data reader is already open on the same connection.

Application Code

The following code uses the entities and associations defined in the conceptual schema and mapped to storage metadata in the mapping specification. For details on this data model, see Northwind Model (EDM). An EntityConnection is opened to the ObjectContext created built from the conceptual schema defined in the topic Northwind Conceptual Schema (EDM). The entities and the association in the NorthwindLib namespace are used to display properties of entities such as sales-order identifiers, find customers from associations between entities, display products, and to find and display product categories related to products by associations.

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

namespace NorthwindClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (NorthwindLib.Northwind DB = new Northwind())
                {
                    foreach (SalesOrder salesOrder in DB.SalesOrders)
                    {
                        salesOrder.CustomerReference.Load();
                        Console.WriteLine(
                            "Customer Contact: {0} ** OrderID: {1}",
                            salesOrder.Customer.ContactName,
                            salesOrder.OrderID.ToString());
                    }

                    foreach (Product product in DB.Products)
                    {
                        product.CategoryReference.Load();
                        Console.WriteLine(" Product: " +
                            product.ProductName +
                            " ** Category: " + 
                            product.CategoryReference.Value.CategoryName);
                    }

                    DB.Connection.Close();
                }
            }

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

        }
    }
}

See Also

Other Resources

Schemas and Mapping Specification (Entity Framework)
EDM Specifications