Connection handling to execute database queries its

Info icon This preview shows pages 595–597. Sign up to view the full content.

View Full Document Right Arrow Icon
Connection Handling To execute database queries, it’s necessary to connect to a database, so the object con- text 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 appli- cation does not already have one, and then it adds a connection string. Exam- ple 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 Exam- ple 14-1 , because the Entity Framework needs three things in its connection string: information on where to find the EDM definition, the type of underlying database provider to use, and the connection string to pass to that underlying provider. This last Object Context | 571
Image of page 595

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
part—an ordinary SQL Server connection string, enclosed in &quot; character entities—is highlighted in Example 14-13 in bold. The three URIs in the metadata section of the connectionString —the ones beginning with res:// —point to the three parts of the EDM: the conceptual schema, the storage schema, and the mappings. Visual Studio extracts these from the .edmx file and embeds them as three XML resource streams in the compiled program. Without these, the EF wouldn’t know what the conceptual and storage schemas are supposed to look like, or how to map between them. It may seem a bit weird for the locations of these EDM resources to be in a connection string. It might seem more natural for the XML to use a separate attribute for each one. However, as you’ve seen, the System.Data.EntityClient namespace conforms to the ADO.NET v1 model so that it’s possible for old-style data access code to perform queries against the EDM. Since the ADO.NET v1 model includes an assumption that it’s possible to put all the information defining a par- ticular data source into a single connection string, the Entity Framework has to follow suit. And since the EF cannot function without the XML EDM definitions, the connection string has to say where those live. After the EDM metadata resources, you can see a provider property, which in Exam- ple 14-13 indicates that the underlying database connection is to be provided by the SQL Server client. The EF passes the provider connection string on to that provider.
Image of page 596
Image of page 597
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern