Is it possible to add a default where clause to every SQL statement generated by a Linq-to-SQL class?
I have a custom DataContext with a Customer class. The Customer class has a Deleted attribute, which is what I want to always be NULL whenever I query the table.
So for example, I could write:
List<Customer> customers = db.Customers.ToList<Customer>();
But really get:
List<Customer> customers = db.Customers.Where(o => o.Deleted == null).ToList<Customer>();
I want to maintain the “deleted” data in my DB, but will never need to see it in my .NET code. This sort of default would be handy, so I don’t have to remember to add the filter to every query.
Instead of adding the table to your dbml designer, add a view that contains the default where clause, and just name the view Customer in your dbml.
To allow updates, inserts, and deletes, make sure you select the primary key columns in your DBML designer, and mark their
Primary Keyproperty as true.