在EF中的ObjectMaterialize不是在第一级查询上触发 [英] ObjectMaterialize in EF not firing on first level query

查看:144
本文介绍了在EF中的ObjectMaterialize不是在第一级查询上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询,如:

 查询语法1  - 不触发某个处理程序; 
var results =(从我在db.mytable
中选择新的myObject(){
column1 = i.Prop1
})ToList();

查询语法2 - 触发somehandler事件;
var results =(从我在db.mytable
中选择I).toList();我的ContextClass中有

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

我看到的唯一区别是第一个查询从选择结果构建一个新对象。 p>

任何意图为什么事件不会触发?

解决方案

事件仅为实体对象投影而触发,这就是为什么您看到这种行为。


如果查询使用投影并且没有匹配的实体,结果将实现为DbDataRecords(或使用LINQ to Entities查询时的匿名类型)而不是实体对象,参考编程实体框架(Julia Lerman)P-244)


ObjectMarialized状态的定义


当新的


参考资料。 https:// msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.objectmaterialized(v=vs.110).aspx



自投影不会创建Entity对象,它不会触发事件。


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?

解决方案

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.

这篇关于在EF中的ObjectMaterialize不是在第一级查询上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆