简体   繁体   English

如何防止EF检索某些对象

[英]How to to prevent EF from retrieving certain objects

Excuse me for my broken English. 打扰一下我的英语不好意思。

In my application, all objects in the context have a property called ObsoleteFlag, which basically means if the object should still be used on the frontend. 在我的应用程序中,上下文中的所有对象都有一个称为ObsoleteFlag的属性,这基本上意味着该对象是否仍应在前端使用。 It's some sort of "soft-delete" flag without actually having to delete the data. 这是某种“软删除”标志,而实际上不必删除数据。

Now I want to prevent EF from returning any object where ObsoleteFlag is set to true (1) 现在,我想防止EF返回ObsoleteFlag设置为true的任何对象(1)

If for example I retrieve object X, the navigational list property Y contains all the related objects of type Y, no matter what the ObsoleteFlag is set to. 例如,如果我检索对象X,则无论ObsoleteFlag设置为什么,导航列表属性Y都包含类型Y的所有相关对象。

Is there some general way of preventing EF from doing this? 有什么通用的方法可以阻止EF这样做吗? I don't want to check on the ObsoleteFlag property everywhere I access the context, and for every navigational property that may be loaded too. 我不想在访问上下文的任何地方检查ObsoleteFlag属性,也不想检查可能也会加载的每个导航属性。

Thanks and sorry for my broken English. 谢谢,抱歉我的英语不好。

Two different approaches: 两种不同的方法:

  1. In your repository layer have a GetAllWhatever() that returns IQueryable<Whatever> and uses Where(x => !x.Obsolete) and use this whenever you retrieve objects of this type. 在您的存储库层中,有一个GetAllWhatever() ,它返回IQueryable<Whatever>并使用Where(x => !x.Obsolete)并在检索此类型的对象时使用它。

  2. Create a view of Create View ActiveWhatever As Select * from ActiveWhatever Where obsolete = 0 and bind to that rather than the table. 创建视图的Create View ActiveWhatever As Select * from ActiveWhatever Where obsolete = 0并绑定到该Create View ActiveWhatever As Select * from ActiveWhatever Where obsolete = 0而不是表。

The first is essentially checking the flag every time, but doing so in one place, so you don't have to keep thinking about it. 首先本质上是每次都检查标志,但是要在一个地方进行,因此您不必继续考虑它。

The second is much the same, but the work is pushed to the database instead of the .NET code. 第二个基本相同,但是工作被推送到数据库而不是.NET代码。 If you are going to modify the entities or add new entities you will have to make it a modifiable view, but just how that is done depends on the database in question (eg you can do it with triggers in SQL Server, and triggers or rules in PostgreSQL). 如果要修改实体或添加新实体,则必须使其成为可修改的视图,但具体操作方式取决于所讨论的数据库(例如,您可以使用SQL Server中的触发器以及触发器或规则来做到这一点。在PostgreSQL中)。

The second can also include having a rule or trigger for DELETE that sets your obsolete property instead of deleting, so that a normal delete as far as Entity Framework is concerned becomes one of your soft-deletes as far as the database is concerned. 第二个还可以包括有一个DELETE规则或触发器,该规则或触发器设置您的过时属性而不是删除,以便就实体框架而言,普通删除就数据库而言成为您的软删除之一。

I'd go for that approach unless you had a reason to object to a view existing just to help the application's implementation (that is you're heavily into the database being "pure" in being concerned with the data rather than its use). 我会采用这种方法,除非您有理由反对现有的视图只是为了帮助应用程序的实现(也就是说,您沉迷于数据库中纯粹是在关注数据而不是数据的使用)。 But then, if it's handy for one application it's likely handy for more, given the very meaning of this "obsolete". 但是,如果考虑到这种“过时”的含义,那么如果它对于一个应用程序很方便,那么对于更多的应用程序可能就很方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM