c# - How to use the interception mechanism in Entity Framework 6.3? -


we need add new functionality in our app, such select queries performed our dbcontext on dbsets, example when calling mydbcontext.users, or mydbcontext.students etc (user , student inherit baseentity class, includes propery isactive), able intercept them, check if entity of base type baseentity, , modify queries in way, example add clause check if isactive true. i've tried idbcommandinterceptor interface methods, including 1 relevent me, readerexecuting, intercept kinds of select statements performed on database, not ones mydbcontext's dbsets.
how can in correct way?

thanks, ashilon

interceptors, in instance, not want. intercept queries dbcontexts , can modify sql directly. dangerous when want add clause because joins mess up.

if have repository or base class queries go through, there. if doing context.students.where(...) there sneaky thing can do, not entirely sure if this'll work don't see why wouldn't.

on dbcontext class, change dbset property namesd, students, studentsdbset. add property replace it:

public iqueryable<student> students  {      { return studentdsdbset.where(a => a.isactive); } } 

all old code refer property forces isactive records. if needed non-active students, studentsdbset.where(...) , you'd go.


Comments