Friday, 15 July 2011

c# - How can I "warm up" a LinqToSql precompiled query? -



c# - How can I "warm up" a LinqToSql precompiled query? -

in linqtosql, precompiled queries great - still requires me take compilation performance nail first time query used.

i 'warm up' these precompiled queries in background when application starts up. can calling them default parameters - results in needless nail database.

is there way 'warm up' precompiled query without calling out database?

i have taken @ compliedquery source code seems many of classes needed sealed and/or internal...

ok, looking @ source code compiledquery gets returned compiledquery.compile() can verify query compiled 1 time invoke method first time:

icompiledquery compiled; private object executequery(datacontext context, object[] args) { if (context == null) { throw error.argumentnull("context"); } if (this.compiled == null) { lock (this) { if (this.compiled == null) { // query copmiled this.compiled = context.provider.compile(this.query); this.mappingsource = context.mapping.mappingsource; } } } // query executed against info store homecoming this.compiled.execute(context.provider, args).returnvalue; }

so there no way forcefulness compilation without executing it.

however, did find hacky workaround gives me need. there way can execute query causing query precompiled without allowing phone call database:

// compiled query static func<northwinddatacontext, int, iqueryable<order>> getorderbyid = compiledquery.compile((northwinddatacontext db, int orderid) => linq goes here ); static void warmup() { var context = new northwinddatacontext("connectionstring"); // dispose connection forcefulness attempted database phone call fail context.connecction.dispose(); seek { getorderbyid(context, 1); } grab (exception ex) { // expect find invalidoperationexception message // "the connectionstring property has not been initialized." // underlying query compiled } } static void getdata() { // long called warmup first, not take compilation performance nail var context = new northwinddatacontext("connectionstring"); var result = getorderbyid(context, 1); }

c# linq-to-sql compiled-query

No comments:

Post a Comment