Danh mục

Pro Entity Framework 4.0 - Apress_2

Số trang: 26      Loại file: pdf      Dung lượng: 929.15 KB      Lượt xem: 6      Lượt tải: 0    
Thư viện của tui

Xem trước 3 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Chương này sẽ thảo luận làm thế nào để viết các truy vấn chống lại các EDM bằng cách sử dụng cú pháp LINQ-to-Các đối tượng và thực thể cú pháp SQL, được cung cấp bởi Entity Framework. Chương này cũng sẽ thảo luận về sự khác biệt giữa cú pháp truy vấn và cú pháp phương pháp và khi bạn có thể sử dụng một trong khác.
Nội dung trích xuất từ tài liệu:
Pro Entity Framework 4.0 - Apress_2CHAPTER 4■■■Querying the EDMYou have spent the previous two chapters creating and exploring an Entity Data Model. Chapter 2discussed the different ways an EDM can be created, and Chapter 3 explored the many facets of the EDMboth internally and externally. It is now finally time to write some code. This chapter will discuss how to write queries against the EDM by using the LINQ-to-Entities syntaxand the Entity SQL syntax, both provided by the Entity Framework. This chapter will also discuss thedifference between query syntax and method syntax and when you might use one over the other. We’llalso spend a few pages discussing how queries are executed so that you can write effective queries foroptimal performance. We won’t go too deep in this chapter, since we’ll save the more advanced topics for later. Theimportant thing this chapter will do will be to build a foundation you can use for writing and optimizingqueries.Querying with the Entity FrameworkThe key to remember when working and querying with the Entity Framework is that you are querying adata model, not directly against a database. Over the last couple of chapters you have created severalEDMs, and in this chapter you are going to query against the EDM. This is much different than queryingdirectly against a database, for several reasons. First, the syntax is different. Instead of writing T-SQLqueries, you will use LINQ to Entities or Entity SQL to construct and execute queries. Second, when youquery the EDM you are letting the Entity Framework do a lot of the work for you, such as processing yourqueries and handling results. The Entity Framework employs the ADO.NET providers to handle query operations. Specifically, theSystem.Data.SqlClient is utilized to turn your query into something the SQL Server database engine willunderstand. On the return side, this same provider will do the work of translating the results into objectsthat your application can work with. I know by now you are itching to starting querying, so let’s get to that.Syntax OptionsWhen writing queries to query your EDM, there are several syntax options available to you. Before youbegin writing queries it will be helpful to know the differences between the two syntaxes and why youwould use one or the other. Thus, the following two sections will discuss the available syntaxes, queryexpression and method-based.Query-Expression SyntaxThe most common syntax used with writing LINQ queries (LINQ to Entities, LINQ to SQL, etc.) is thequery-expression syntax. This is simply because it is easier to read and understand. With query- 63CHAPTER 4 ■ QUERYING THE EDM expression syntax, queries are written using operators and functions. This section will spend a page or two showing some examples of query expression, and most if not all of the examples throughout this book will use this syntax. For your first example, open Form1.cs in design mode and place a button and list box on the form. In the Click event of the button, place the following code: using (var context = new AdventureWorks2008Entities()) { var people = context.People; foreach (var person in people) { listBox1.Items.Add(string.Format({0} {1}, person.FirstName, person.LastName)); } } Run the code by pressing F5. When the form appears, click the button. After several seconds, the list will populate, as shown in Figure 4-1. Figure 4-1. First query If this is your very first LINQ-to-Entities query, congratulations. Let’s spend a few minutes looking at the syntax. The very first line we are interested in is the following: var context = new AdventureWorks2008Entities() In Chapter 3 you learned all about the context and how the context is used in the Entity Framework. The context is discussed again shortly. The next line we want to look at is this one: var people = context.People64 CHAPTER 4 ■ QUERYING THE EDM This line is the query itself. This is the simplest form of a query. The last line executes the query andthen iterates through the results.foreach (var person in people) Before we move on, I need to point out a change between .NET 3.5 Entity Framework and .NET 4.0Entity Framework. Put a breakpoint on the closing brace (}) of the foreach block. Run the app again andclick the button. When the execution hits the breakpoint, hold your mouse pointer over the word peoplein the foreach line. You’ll notice that the type is a System.Data.Objects.ObjectSet.Figure 4-2. ObjectSet The ObjectSet class lets you work with ...

Tài liệu được xem nhiều: