Object with default filter for EF

I have an object called Client in my .edmx file.

I need to run some linq queries again, but for all of them I need a filter (say active = 1).

I do not want to have where c.active == 1 for all my queries, but it makes sense that my object has a default filter. Is this something that can be done?

Or maybe I can get a query as a base for an object instead of a table? (I am the first approach to the database)

I know that another solution would be to create a view of the database and associate the entity with the view, but I don’t want that either.

+4
source share
1 answer

You can add an additional method to your generated database context:

 partial class DatabaseContext // same name as your generated context { IQueryable<Client> ActiveClients { get { return Clients.Where(c => c.active == 1); } } } 

And use it in all the code. Since it is part of a partial class (defined in some other file than your generated context class), it does not affect the codegen tool when updating your schema.

+2
source

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


All Articles