I came across what seems to be an error in linq for sql, where identifier caching does not work when executing primary key queries inside a compiled query.
I wrote the following example to demonstrate the use of identity caching. It makes only one database call on the first hit, each time after that it retrieves the client object from the data context cache.
for(int i=0; i<10; i++) { DataContext.GetTable<Customer>().Single(c=>c.Id == 1); }
Unfortunately, when I convert the above sample into a compiled query, it cannot use the identifier cache and actually makes 10 calls in the database.
for(int i=0; i<10; i++) { RetrieveCustomer(DataContext, 1); } private static readonly Func<DataContext, int, Customer> RetrieveCustomer = CompiledQuery.Compile((DataContext context, int id) => context.GetTable<Customer>().Single(c=>c.Id == id));
Has anyone else encountered this problem and created a workaround? It is imperative that server applications use compiled requests and identifier caching, so I hope this is a problem that someone else has worked with before!
source share