c# - Entity framework 6 lazy loading oddity -
i have poco class wired entity framework. class called personalizationcatalogprice. class has sub object called pricelevel not have virtual keyword on it. because of (based on understanding) object should never lazy load , should return null. however, have found object seems load when used in specific manner.
so basic class structure this
public class personalizationcatalogprice { public int personalizationcatalogpriceid { get; set; } public int personalizationcatalogid { get; set; } public virtual personalizationcatalog personalizationcatalog { get; set; } public decimal customerretail { get; set; } public decimal consultantcost { get; set; } public decimal personalvolume { get; set; } public decimal qualifyingvolume { get; set; } public int pricelevelid { get; set; } public pricelevel pricelevel { get; set; } }
here example. in first line return iqueryable of personalizationcatalogprice items. in second line have iqueryable , using priclevel object in iqueryable (see line "contains pcp.pricelevel"). when this, pricelevel object loads though has no virtual keyword.
iqueryable<personalizationcatalogprice> allperspricerecords = _e2reposman.personalizationrepository.getsomerecords(); //list<personalizationcatalogprice> persprices = allperspricerecords.tolist(); var persquery = pl in personalizationlines join pcp in allperspricerecords on pl.item2.personalizationcatalogpriceid equals pcp.personalizationcatalogpriceid !hostesspriceleveltypes().contains(pcp.pricelevel.priceleveltypeid) select pl.item2.personalvolume * pl.item1; var openorderstotal = (query.any() ? query.sum() : 0) + (persquery.any() ? persquery.sum() : 0);
modifying code uncommenting second line performs tolist() however. makes pricelevel subobject return null expectedd since not have virtual keyword on , entity framework works anticipate. (ef 6)
does know why able load pricelevel property when not tolist().
thanks assistance.
based on wording used, appears expect pricelevel
property null
when not using lazy loading.
there cases can think of, in expectation not met.
the first case if loading data in eager way. explicitly using include()
. behaviour called eager loading. since eager loading load allowed to, not require enumerate collection calling toarray()
or tolist()
or other methods trigger enumeration. every property not lazy loaded, includes value type properties , non-lazy properties.
your code not indicate this, property still not null if did on same dbcontext
derived instance before executing code displayed. happen whenever entity in question resides cached in dbcontext.
the same true if had loaded entity explicitly calling load()
or if had been returned other previous query.
in order verify whether case or not can use asnotracking() extension method. entities not tracked will, among other effects, not linked each other.
Comments
Post a Comment