如何:禁用推迟加载

可以通过将 DeferredLoadingEnabled 设置为 false 来关闭延迟加载。 有关详细信息,请参阅延迟加载与立即加载

备注

关闭对象跟踪时,也隐式地关闭了延迟加载。 有关详细信息,请参阅如何:将信息检索为只读

示例

下面的示例演示如何通过将 DeferredLoadingEnabled 设置为 false 来关闭延迟加载。

Northwnd db = new Northwnd(@"c:\northwnd.mdf");
db.DeferredLoadingEnabled = false;

DataLoadOptions ds = new DataLoadOptions();
ds.LoadWith<Customer>(c => c.Orders);
ds.LoadWith<Order>(o => o.OrderDetails);
db.LoadOptions = ds;

var custQuery =
    from cust in db.Customers
    where cust.City == "London"
    select cust;

foreach (Customer custObj in custQuery)
{
    Console.WriteLine("Customer ID: {0}", custObj.CustomerID);
    foreach (Order ord in custObj.Orders)
    {
        Console.WriteLine("\tOrder ID: {0}", ord.OrderID);
        foreach (OrderDetail detail in ord.OrderDetails)
        {
            Console.WriteLine("\t\tProduct ID: {0}", detail.ProductID);
        }
    }
}
Dim db As New Northwnd("c:\northwnd.mdf")

db.DeferredLoadingEnabled = False

Dim ds As New DataLoadOptions()
ds.LoadWith(Function(c As Customer) c.Orders)
ds.LoadWith(Of Order)(Function(o) o.OrderDetails)
db.LoadOptions = ds

Dim custQuery = From cust In db.Customers _
                Where cust.City = "London" _
                Select cust

For Each custObj In custQuery
    Console.WriteLine("Customer ID: {0}", custObj.CustomerID)
    For Each ord In custObj.Orders
        Console.WriteLine(vbTab & "Order ID: {0}", ord.OrderID)
        For Each detail In ord.OrderDetails
            Console.WriteLine(vbTab & vbTab & "Product ID: {0}", _
                detail.ProductID)
        Next
    Next
Next

请参阅