如何:将信息作为只读信息检索

当您不打算更改数据时,您可以通过设法产生只读结果来提高查询性能。

通过将 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

请参阅