Page 1 of 1

Retrieving Objects from the Identity Cache

Posted: Sun 26 Aug 2012 11:52
by usix
Does linqConnect try to retrieve an object from the identity cache before executing a query in the database?

Link2sql has that possibility. http://msdn.microsoft.com/en-us/library ... 10%29.aspx

I'v tried to get the same behaviour with linqConnect, but it seems that linqConnect allways executes a query in the databese.

Re: Retrieving Objects from the Identity Cache

Posted: Mon 27 Aug 2012 12:58
by StanislavK
LinqConnect should work in the very same way as it is described in the provided article: if you are querying for a single entity by its primary key, LinqConnect searches for it in the entity cache at first. However, if there is no such entity in the cache, it has to query the database.

For example, consider the following code:

Code: Select all

MyDataContext context = new MyDataContext()
  { Log = Console.Out };

// Since the cache is empty, we query the database at this point.
Dept first = context.Depts
  .OrderBy(d => d.Deptno)
  .First();

// No SQL is executed now, because the entity with the smallest key is already cached.
Dept first1 = context.Depts
  .Where(d => d.Deptno == first.Deptno)
  .SingleOrDefault();

// The cache isn't empty, but no entity with the specified key was found.
// Hence has to search for it in the database.
Dept second = context.Depts
  .Where(d => d.Deptno == first.Deptno + 10)
  .SingleOrDefault();
If you are observing some other behaviour, please describe the situation in more details.

Re: Retrieving Objects from the Identity Cache

Posted: Mon 27 Aug 2012 18:57
by usix
I've put a query directly in SigleOrDefault

Code: Select all

Dept first1 = context.Depts
  .SingleOrDefault(d => d.Deptno == first.Deptno);
and it doesn't work, but your variant works pretty well.

Also it would be great if you fix another annoying bug. It is impossible to retrieve an object from the Identity Cache if type of the primary key is short or byte. It happens becouse of a typecast that C# compiller add to expression. Lets pretend that Deptno is short. C# compiller automatically convert lamdla d => d.Deptno == first.Deptno to d=> (int)d.Deptno == (int)first.Deptno This case isn't processed correctly neither linq2sql nor linqconnect

Thanks a lot

Re: Retrieving Objects from the Identity Cache

Posted: Tue 28 Aug 2012 11:57
by StanislavK
Thank you for clarification, we have reproduced the issue. We will inform you when it is fixed.

Re: Retrieving Objects from the Identity Cache

Posted: Wed 29 Aug 2012 16:12
by StanislavK
We have fixed the problem with getting entities directly from the cache for commands like
'context.Depts.First(d => d.Deptno == literal)'.

Also, we have implemented getting entities by keys of type Int16.

These changes will be available in the nearest build. We will post here when this build is released.

Re: Retrieving Objects from the Identity Cache

Posted: Fri 07 Sep 2012 06:25
by MariiaI
New build of LinqConnect 4.1.82 is available for download now!
It can be downloaded from http://www.devart.com/linqconnect/download.html (trial version) or from Registered Users' Area (for users with active subscription only).
For more information, please refer to http://forums.devart.com/viewtopic.php?f=31&t=24844