EF 中的 ObjectMaterialize 未在第一级查询上触发
ObjectMaterialize in EF not firing on first level query
我有一个查询:
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();
在我的 ContextClass 中我有这样的东西:
((IOjectContextAdapter)this).ObjectContext.ObjectMaterialized += somehandler;
我看到的唯一区别是第一个查询根据 select 结果构建了一个新对象。
知道为什么事件不会触发吗?
事件仅针对实体对象投影触发,这就是您看到此行为的原因。
"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)
ObjectMarialized 状态的定义
Occurs when a new entity object is created from data in the data source as part of a query or load operation.
由于投影不创建实体对象,因此不会触发事件。
我有一个查询:
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();
在我的 ContextClass 中我有这样的东西:
((IOjectContextAdapter)this).ObjectContext.ObjectMaterialized += somehandler;
我看到的唯一区别是第一个查询根据 select 结果构建了一个新对象。
知道为什么事件不会触发吗?
事件仅针对实体对象投影触发,这就是您看到此行为的原因。
"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)
ObjectMarialized 状态的定义
Occurs when a new entity object is created from data in the data source as part of a query or load operation.
由于投影不创建实体对象,因此不会触发事件。