The process of loading related entities eventhough Lazy loading is disabled is called as Explicit loading.
Suppose you have disabled lazy loading.But you want to override this functionality,is it possible.
Yes, it is possible making explicit call.
Using (var context=new CustomerDBEntities())
{
var cust=(from c in context.customers
.where c.customername=='john'
.select c).FirstorDefault();
context.Entry(customer).Reference(c=>c.CustomerAddress).Load();
}
//EXPLICIT LOADING:
//SCENARIO 1: LOAD ALL ORDERS FOR THE CUSTOMER
Console.WriteLine("Each CUSTOMER have MULTIPLE ORDERS");
Customer cust = context.customer.First();
context.Entry(cust).Collection("Orderrs").Load();
Console.WriteLine("customerid{0},customername{1}",cust.CustomerID,cust.CustomerName);
Console.ReadLine();
foreach (var ord in cust.Orderrs)
{
Console.WriteLine("customerid{0} ", ord.Customer.CustomerID);
Console.WriteLine("customername{0} ", ord.Customer.CustomerName);
Console.WriteLine("orderid{0} ", ord.OrderID);
Console.WriteLine("orderdate{0} ", ord.OrderDate);
Console.ReadLine();
}
//SCENARIO 2: Each ORDER CAN have One customer
Note:
For Explicit Loading , no need to mark navigation property as Virtual
Console.WriteLine("Each ORDER CAN have One customer");
Orderr ord1 = context.orderr.FirstOrDefault(m=>m.OrderID==6);
context.Entry(ord1).Reference(m => m.Customer).Load();
Console.WriteLine("{0} {1} ", ord1.Customer.CustomerName,ord1.OrderID );
Console.ReadLine();
EXAMPLE 1: EXPLICIT LOADING WITHOUT FILTERING:
For example, Load all orders :-
//IT ITERATES through LIST OF CUSTOMERS TO GET the LIST OF ORDERS UNDER THAT CUSTOMER.
List
EXAMPLE 2: EXPLICIT LOADING WITH FILTERING:
For example, Load all orders for specific customer :-
Note:
The query method filters the collection first, before load method.
//IT ITERATES through LIST OF CUSTOMERS TO GET the LIST OF ORDERS UNDER THAT CUSTOMER.
List