作法:以唯讀形式擷取資訊
不想要變更資料時,可以搜尋唯讀結果以增加查詢效能。
將 ObjectTrackingEnabled 設定為 false
,即可實作唯讀處理。
注意
ObjectTrackingEnabled 設定為 false
時,DeferredLoadingEnabled 會隱含地設定為 false
。
範例
下列程式碼會擷取員工雇用日期的唯讀集合。
Northwnd db = new Northwnd(@"c:\northwnd.mdf");
db.ObjectTrackingEnabled = false;
IOrderedQueryable<Employee> hireQuery =
from emp in db.Employees
orderby emp.HireDate
select emp;
foreach (Employee empObj in hireQuery)
{
Console.WriteLine("EmpID = {0}, Date Hired = {1}",
empObj.EmployeeID, empObj.HireDate);
}
Dim db As New Northwnd("c:\northwnd.mdf")
db.ObjectTrackingEnabled = False
Dim hireQuery = _
From emp In db.Employees _
Select emp _
Order By emp.HireDate
For Each empObj As Employee In hireQuery
Console.WriteLine("EmpID = {0}, Date Hired = {1}", _
empObj.EmployeeID, empObj.HireDate)
Next