c# - Learning Entity Framework 6 command tree interceptors -


this purely learning experiment (yay science!). not meant used anywhere. want learn how ef6's command tree interceptors work.

i'm trying modify intercepted command tree add "isactive = 1" filter queries. i've noticed serious deficiency in documentation on sort of thing. 2 questions:

how selectively intercept command trees say, entities implement interface (such ihasanactiveproperty)? right note interceptor intercepting queries history context, has nothing myentity.

how add filter queries?

public class myentity {     [databasegenerated(databasegeneratedoption.identity)]     public int id { get; set; }      public string name { get; set; }      public bool isactive { get; set; } }  public class mydbcontext : dbcontext {     public dbset<myentity> entities { get; set; }      public mydbcontext() : base("default") { }       public mydbcontext(string nameorconnectionstring)         : base(nameorconnectionstring) {} }  public class myconfiguration : dbconfiguration {     public myconfiguration()     {         addinterceptor(new myinterceptor());     } }  public class myinterceptor : idbcommandtreeinterceptor {     public void treecreated(dbcommandtreeinterceptioncontext interceptioncontext)     {         var query = interceptioncontext.result dbquerycommandtree;         if (query != null)         {             debug.writeline("##################");             debug.writeline("dataspace: {0}", interceptioncontext.result.dataspace);             debug.writeline(query.tostring());              query.query.accept(new myvisitor());         }     } } 

this meat of question lies. can see i've identified few things need included, don't have clue how put these things together.

public class myvisitor : basicexpressionvisitor {     public override void visit(dbfilterexpression expression)     {         // add [isactive] = 1         /* building blocks:          *      dbexpressionbuilder.equal          *      dbexpressionbuilder.true          *      dbexpressionbuilder.and   --- when filter expression exists          */          var isactiveproperty = expression.property("isactive");         var equalexp = dbexpressionbuilder.equal(isactiveproperty, dbexpressionbuilder.true);     } } 

supposing had this:

class program {     static void main(string[] args)     {         using (var ctx = new mydbcontext())         {             var entities = ctx.entities.firstordefault(x => x.name == "amy");             console.writeline(entities);         }     } } 

this query results in command tree:

dbquerycommandtree |_parameters |_query : collection{record['id'=edm.int32, 'name'=edm.string, 'isactive'=edm.boolean]}   |_project     |_input : 'limit1'     | |_limit     |   |_filter     |   | |_input : 'extent1'     |   | | |_scan : codefirstdatabase.myentity     |   | |_predicate     |   |   |_     |   |     |_'amy'     |   |     |_=     |   |     |_var(extent1).name     |   |_1     |_projection       |_newinstance : record['id'=edm.int32, 'name'=edm.string, 'isactive'=edm.boolean]         |_column : 'id'         | |_var(limit1).id         |_column : 'name'         | |_var(limit1).name         |_column : 'isactive'           |_var(limit1).isactive 

but don't understand go here.

i used following references me implement custom "soft delete" interception mechanism :

hope help


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -