是否有办法检查实体框架是否填充实体? [英] Is there are way to check if entity framework is populating an entity?

查看:67
本文介绍了是否有办法检查实体框架是否填充实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以检查EF是否在实体上设置属性?我计划使用Postsharp方面向我的实体上的某些属性添加安全属性,以便在当前用户没有相应的
权限时我可以阻止它们更新。但是,我仍然需要EF在实现实体时能够填充属性。似乎ObjectContext.ObjectMaterialized事件是我需要的一半。使用该事件我知道EF何时完成了一个对象,但是
有一种方法可以知道它何时开始进程?

Is it possible to check if the EF is setting properties on an entity? I was planning on using Postsharp aspect to add a security attributes to certain properties on my entity so that I can stop them being updated if the current user does not have the appropriate permissions. However, I still need the EF to be able to populate properties when it is materialising an entity. It seems that the ObjectContext.ObjectMaterialized event is half of what I need. Using that event I know when EF is done with an object, but is there a way to know when it is starting the process?

这个想法是能够设置我的对象上有一些内部标志,以便我的安全方面知道EF正在进行更新。

The idea is to be able to set some internal flag on my object so that my security aspect knows that EF is doing the update.

预期的最终结果是这样的类:

The intended end result is a class something like this:


public class SomeType
{
  public int Id { get; set; }

  [Secured()]
  public string SecuredProperty { get; set; }

  public string AnotherProperty { get; set; }
}

推荐答案

没有事件可以知道EF什么时候开始实现。在未来,我们希望添加更多钩子来控制实体的构造方式等,但目前还没有办法做到这一点。你可以假设一个对象是'不安全的'
,直到EF实现它或者直到它被添加到EF上下文中?一旦将实体添加到上下文中,就可以使用ObjectContext.ObjectStateManager.ObjectStateManagerChanged事件来验证和保护实体。

There isn't an event to know when EF is beginning materialization. In the future we are looking to add more hooks to control how entities are constructed etc but for the moment there isn't a way to do this. Could you just assume that an object is 'unsecured' until EF has materialized it or until it is added to the EF context? You could use the ObjectContext.ObjectStateManager.ObjectStateManagerChanged event to validate and secure the entity once it is added to the context.

~Rowan


这篇关于是否有办法检查实体框架是否填充实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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