ObjectMaterialize in EF not firing on first level query

邮差的信 提交于 2019-12-11 03:05:20

问题


I have a query such as:

Query Syntax 1 - Does not fire the somehandler;
var results = (from I in db.mytable
              select new myObject() {
                        column1 = i.Prop1
              }).ToList();

Query Syntax 2 - Does fires the somehandler event;
var results = (from I in db.mytable
               select I).toList();

in my ContextClass I have something like this:

((IOjectContextAdapter)this).ObjectContext.ObjectMaterialized +=  somehandler;

The only difference I see is that the first query builds a new object from the select results.

Any idea why the event wouldn't fire?


回答1:


The event fires only for Entity object projections, that is why you see this behaviour.

"If the query used a projection and there is no matching entity, the results are materialized into DbDataRecords (or anonymous types when a LINQ to Entities query was used) instead of entity objects," Ref -Programming Entity Framework (Julia Lerman)P-244)

The definition for ObjectMarialized states

Occurs when a new entity object is created from data in the data source as part of a query or load operation.

Ref. https://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.objectmaterialized(v=vs.110).aspx

Since the projection does not create an Entity object, it does not fire the event.



来源:https://stackoverflow.com/questions/29778021/objectmaterialize-in-ef-not-firing-on-first-level-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!