Click here to Skip to main content
14,124,595 members
Rate this:
Please Sign up or sign in to vote.
See more: , +
Hello dears,

I have a situation as follows:
I want to alter all get queries in my application to skip deleted records which is marked as deleted in DB.

Can i configure the context to exclude to do that instead of altering all queries?


What I have tried:

i have searched a lot but i didn't get any result.
I 've also tried to edit all get queries but this will take a lot of time.
Updated 22-Apr-18 22:51pm
F-ES Sitecore 23-Apr-18 4:44am
There might be something clever with EF that can do this, but generally these kind of things are handled by using a repository pattern which allows you to have a single point of code where a "get" is done (and other functions can use that function as a basis of their own queries) so filtering out deleted records is just a case of changing one method. However if your code is not using a repo pattern it's going to involve a lot of refactoring.
_ProgProg_ 23-Apr-18 4:46am
Yes but unfortunately i am not using repository pattern.
Richard Deeming 24-Apr-18 9:39am
How much control do you have over your database, or your EF mapping?

You could simply create views in the database corresponding to each table, which filter out the soft-deleted records. You could then map your EF sets to the views instead of the underlying tables.
_ProgProg_ 24-Apr-18 9:41am
can you give me a detailed example?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Can i configure the context to exclude to do that instead of altering all queries?

No, you have to alter your queries.

For further details, please see:
Entity Framework Working with DbContext[^]
Entity Framework Querying and Finding Entities[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190518.1 | Last Updated 23 Apr 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100