Entity Framework, soft-deleting and queries

So here is my situation:

I gently delete some rows in the table using the IsDeleted flag IsDeleted that traces of my archive data can be saved. I do this by overriding the SaveChanges statement in my ObjectContext .

Question: how can I select only rows that have IsDeleted == false , without specifying && !IsDeleted in each request?

Can this be stated directly in my context?

Tkx!

+6
source share
3 answers

You can define a view from your table and request this view:

 CREATE VIEW dbo.ActiveData AS SELECT (list of columns) FROM dbo.YourTable WHERE IsDeleted = 0 

And then in your EDMX model read your data from the ActiveData view instead of the base table.

+7
source

If you right-click the EntitySet in the model viewer and click "Table Mapping", an area appears where you can "Add Condition". This should do what you ask, although you might be better off using View instead, as suggested by marc_s.

+6
source

This is an old question now, but for any new one coming here. Starting with EF 6, you really need to use interceptors for this type of request. It places the query inside the SQL query during its launch and filters the records based on the flag.

For more information, see the following:

Soft Object Removal Using Interceptors

+1
source

Source: https://habr.com/ru/post/905860/


All Articles