tailieunhanh - Microsoft ADO .NET 4 Step by Step - p 29
Chạy truy vấn SQL thực thể Khung thực thể bao gồm hai cách quan trọng của việc sử dụng thực thể truy vấn SQL để truy cập dữ liệu entitymanaged: sử dụng một thể ObjectQuery để truy vấn các đối tượng trong bối cảnh trực tiếp hoặc bằng cách sử dụng một giao diện cung cấp dịch vụ truyền thống như. | 256 Microsoft 4 Step by Step Running Entity SQL Queries The Entity Framework includes two key ways of using Entity SQL queries to access entitymanaged data using an ObjectQuery instance to query the entities within the context directly or using a more traditional provider-like interface. Running Queries Using an ObjectQuery The Of T class-processes an Entity SQL statement against an open EF context and returns the results as a collection of either named or anonymous instances. C ---- SalesOrderEntities is an Entity Container. using SalesOrderEntities context new SalesOrderEntities GetConnectionStringO ObjectQuery Customer query New ObjectQuery Customer sqlText context . Other code as needed . Visual Basic ------ SalesOrderEntities is an Entity Container. Using context As New SalesOrderEntities GetConnectionString Dim query As New ObjectQuery Of Customer sqlText context . Other code as needed . End Using You must keep the context around as long as you need to access the ObjectQuery object s data especially if the retrieved data includes content accessed through a navigation property. The generic ObjectQuery type works like a typical generic collection object but it s not a true collection. When you create the ObjectQuery instance the Entity Framework delays processing of the query until you specifically request data. Even then it might decide to retrieve only the requested portion of the data. Keeping the context around during the entire data-retrieval process enables the ObjectQuery to fulfill any data requests you give it over time. Chapter 15 Querying Data in the Framework 257 Note The object context and its related entity container in the conceptual model expose a LazyLoadingEnabled property. Changing this Boolean value alters the way that the Entity Framework loads data at the other end of a navigation property. Models built with the visual designer set this property to True by default keeping unused navigation .
đang nạp các trang xem trước