I have a method that map an entity from the context to my domain model like this:
public IQueryable ReadAll()
{
IQueryable result = null;
this.Context.ObjectTrackingEnabled = false;
this.Context.DeferredLoadingEnabled = false;
result = from a in this.Context.CATEGORIES
select new DomainModel.CATEGORIES
{
CATEGORYID = a.CATEGORYID,
CATEGORYNAME = a.CATEGORYNAME,
DESCRIPTION = a.DESCRIPTION,
PICTURE = a.PICTURE,
PRODUCTS = (from b in a.PRODUCTS select new DomainModel.PRODUCTS {
DISCONTINUED = b.DISCONTINUED,
PRODUCTID = b.PRODUCTID,
PRODUCTNAME = b.PRODUCTNAME,
QUANTITYPERUNIT = b.QUANTITYPERUNIT,
REORDERLEVEL = b.REORDERLEVEL,
UNITPRICE = b.UNITPRICE,
UNITSINSTOCK = b.UNITSINSTOCK,
UNITSONORDER = b.UNITSONORDER,
}),
};
return result;
}
Why when I do a linq query over the IQueryable I obtain an object of type DomainModel.CATEGORIES with a collection of type Devart.Data.Linq.Provider.DeferredSource insted of type DomainModel.PRODUCTS?
Thanks
DeferredSource
This is designed behaviour. It is compatible with Microsoft code. Please refer to this article:
http://msdn.microsoft.com/en-us/library ... abled.aspx
http://msdn.microsoft.com/en-us/library ... abled.aspx
deferred loading
In reference to the previous code, there is a difference in the behavior
between Microsoft linq to sql and yours.
The problem is after executing result.ToList() and disposing context:
if we browse PRODUCTS, provider thows an Exception because want to access database although deferred loading is disabled.
Here is sample code with orders and detail
static void test1(){
string connection = "Password=northwind;User ID=northwind;Data Source=ora10g";
MyOrder alfkiOrders;
using (Northwind.Northwind entities =new Northwind.Northwind(connection)) {
entities.ObjectTrackingEnabled = false;
entities.DeferredLoadingEnabled = false;
// -- dal tier
IQueryable qResult = from o in entities.ORDERs
select new MyOrder {
ORDERID = o.ORDERID,
CUSTOMERID = o.CUSTOMERID,
Detail = from d in o.ORDERDETAILs
select new MyOrderDetail() {
ORDERID=d.ORDERID,
PRODUCTID=d.PRODUCTID,
QUANTITY=d.QUANTITY,
},
};
// -- biz tier
alfkiOrders = qResult.Where(order => order.CUSTOMERID == "ALFKI").First();
}
// accessing alfki order detail
foreach (var item in alfkiOrders.Detail) {
item.QUANTITY=22;
// Error!! IEnumerable execute deferred loading after "First()" or "ToList()" operation
}
}
static void test2() {
// trying with a list
string connection = "Password=northwind;User ID=northwind;Data Source=ora10g";
MyOrder alfkiOrders;
using (Northwind.Northwind entities = new Northwind.Northwind(connection)) {
entities.ObjectTrackingEnabled = false;
entities.DeferredLoadingEnabled = false;
// -- dal tier
IQueryable qResult = from o in entities.ORDERs
select new MyOrder {
ORDERID = o.ORDERID,
CUSTOMERID = o.CUSTOMERID,
Detail = new List(
from d in o.ORDERDETAILs
select new MyOrderDetail() {
ORDERID = d.ORDERID,
PRODUCTID = d.PRODUCTID,
QUANTITY = d.QUANTITY,
}
),
};
// -- biz tier
var allOrders = qResult.ToList(); //ok
alfkiOrders = qResult.Where(order => order.CUSTOMERID == "ALFKI").First(); // error
}
// accessing alfki order detail
foreach (var item in alfkiOrders.Detail) {
item.QUANTITY = 22;
// Error!! IEnumerable execute deferred loading after "First()" or "ToList()" operation
}
}
thanks!
between Microsoft linq to sql and yours.
The problem is after executing result.ToList() and disposing context:
if we browse PRODUCTS, provider thows an Exception because want to access database although deferred loading is disabled.
Here is sample code with orders and detail
static void test1(){
string connection = "Password=northwind;User ID=northwind;Data Source=ora10g";
MyOrder alfkiOrders;
using (Northwind.Northwind entities =new Northwind.Northwind(connection)) {
entities.ObjectTrackingEnabled = false;
entities.DeferredLoadingEnabled = false;
// -- dal tier
IQueryable qResult = from o in entities.ORDERs
select new MyOrder {
ORDERID = o.ORDERID,
CUSTOMERID = o.CUSTOMERID,
Detail = from d in o.ORDERDETAILs
select new MyOrderDetail() {
ORDERID=d.ORDERID,
PRODUCTID=d.PRODUCTID,
QUANTITY=d.QUANTITY,
},
};
// -- biz tier
alfkiOrders = qResult.Where(order => order.CUSTOMERID == "ALFKI").First();
}
// accessing alfki order detail
foreach (var item in alfkiOrders.Detail) {
item.QUANTITY=22;
// Error!! IEnumerable execute deferred loading after "First()" or "ToList()" operation
}
}
static void test2() {
// trying with a list
string connection = "Password=northwind;User ID=northwind;Data Source=ora10g";
MyOrder alfkiOrders;
using (Northwind.Northwind entities = new Northwind.Northwind(connection)) {
entities.ObjectTrackingEnabled = false;
entities.DeferredLoadingEnabled = false;
// -- dal tier
IQueryable qResult = from o in entities.ORDERs
select new MyOrder {
ORDERID = o.ORDERID,
CUSTOMERID = o.CUSTOMERID,
Detail = new List(
from d in o.ORDERDETAILs
select new MyOrderDetail() {
ORDERID = d.ORDERID,
PRODUCTID = d.PRODUCTID,
QUANTITY = d.QUANTITY,
}
),
};
// -- biz tier
var allOrders = qResult.ToList(); //ok
alfkiOrders = qResult.Where(order => order.CUSTOMERID == "ALFKI").First(); // error
}
// accessing alfki order detail
foreach (var item in alfkiOrders.Detail) {
item.QUANTITY = 22;
// Error!! IEnumerable execute deferred loading after "First()" or "ToList()" operation
}
}
thanks!