O'Reilly logo

Programming C# 4.0 by Jesse Liberty, Matthew Adams, Ian Griffiths

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Object Context

As you’ve seen, the object context provides access to entities. For each entity we define in our EDM, the generated object context class provides a property that we can use as the source for a LINQ query. We’ve also used its CreateQuery<T> method to build ESQL-based queries. The object context provides some other services.

Connection Handling

To execute database queries, it’s necessary to connect to a database, so the object context needs connection information. This information typically lives in the App.config file—when you first run the EDM wizard, it will add a configuration file if your application does not already have one, and then it adds a connection string. Example 14-13 shows a configuration file containing a typical Entity Framework connection string. (This has been split over multiple lines to fit—normally the connectionString attribute is all on one line.)

Example 14-13. Connection string in App.config

<configuration>
  <connectionStrings>
    <add name="AdventureWorksLT2008Entities"
         connectionString="metadata=res://*/AdventureWorksModel.csdl|
  res://*/AdventureWorksModel.ssdl|res://*/AdventureWorksModel.msl;
  provider=System.Data.SqlClient;provider connection string=
  &quot;Data Source=.\sqlexpress;Initial Catalog=AdventureWorksLT2008;
  Integrated Security=True;MultipleActiveResultSets=True&quot;"
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

This is a rather more complex connection string than the one we saw back in Example 14-1 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required