Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework, soft-deleting and queries

So here's my situation:

I'm soft-deleting some rows in a table, using a IsDeleted flag, so that a can keep a trace of my archived data. I do so by overriding the SaveChanges statement in my ObjectContext.

The question is: how can I select only the row(s) that have IsDeleted == false, without having to specify && !IsDeleted in every queries?

Is there a way I can specify this on my context directly?

tkx!

like image 575
iPeo Avatar asked Jan 12 '12 19:01

iPeo


1 Answers

You could define a view over your table and query that view instead:

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.

like image 132
marc_s Avatar answered Sep 29 '22 02:09

marc_s