实体框架-实现IDbSet [英] Entity Framework - implementing IDbSet

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

问题描述

我想实现IdbSet来为我的DbContext提供一个自定义实现,该实现本质上将用于为当前登录的用户过滤我的数据(我正在处理多租户应用程序).我要使用的总体思路与这篇文章非常相似:可以执行DbContext吗?过滤政策?

I would like to implement IdbSet to provide my DbContext a custom implementation that will essentially serve to filter my data for the currently logged in user (I am working on a multi-tenant application). The general idea I am going with is very similar to this post: Can a DbContext enforce a filter policy?

但是,我不清楚如何使DbContext知道"如何使用自己的实现IDbSet的类.我很难找到有关此问题的文档.任何指针都会有所帮助!

However, it is not clear to me how to make DbContext "know" about how to use my own class that implements IDbSet. I am having a tough time finding documentation on this. Any pointers would be helpful!

TIA, -jle

TIA, -jle

推荐答案

我几乎可以确定您无法创建自己的IDbSet实现并将其传递给实体框架.这样的实现将丢失与EF相关的所有东西,而EF是在DbSet本身内部实现的-在内部,我真的是说没有公共API可以代替它.提供IDbSet接口不是因为它应该创建您自己的集合,而是因为当进行单元测试应用程序时它允许模拟集合.扩展功能的唯一方法是:

I'm almost sure that you cannot create your own implementation of IDbSet and pass it to entity framework. Such implementation will lose all stuff related to EF which is internally implemented in DbSet itself - by internally I really mean that there is no public API to replace it. IDbSet interface is provided not because it is supposed to create your own sets but because it allows mocking sets when unit testing application. The only way you can extend functionality is either:

  • 继承DbSet,但是由于方法/属性不会被标记为virtual,因此恐怕对您也没有帮助.
  • 创建将包装DbSet的自定义IDbSet实现.这看起来是最好的机会,但是您仍然可以发现DbContext不喜欢您的新实现.我进行了非常快速的尝试,但没有成功.我的实现可用于持久化,但不能用于查询.
  • Inheriting DbSet but I'm afraid that it will not help you as well because methods / properties will not be marked as virtual.
  • Creating custom IDbSet implementation which will wrap DbSet. This looks like the best chance but you can still find that DbContext doesn't like your new implementation. I gave this very quick try but I was not successful. My implementation worked for persisting but not for querying.

这篇关于实体框架-实现IDbSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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