本文介绍了在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>

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

解决方案

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

ObjectMarialized状态的定义

参考资料。



自投影不会创建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.

The definition for ObjectMarialized states

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不是在第一级查询上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:06