Retrieving Objects from the Identity Cache

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
usix
Posts: 2
Joined: Sun 26 Aug 2012 11:38

Retrieving Objects from the Identity Cache

Post by usix » Sun 26 Aug 2012 11:52

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.

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Re: Retrieving Objects from the Identity Cache

Post by StanislavK » Mon 27 Aug 2012 12:58

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.

usix
Posts: 2
Joined: Sun 26 Aug 2012 11:38

Re: Retrieving Objects from the Identity Cache

Post by usix » Mon 27 Aug 2012 18:57

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

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Re: Retrieving Objects from the Identity Cache

Post by StanislavK » Tue 28 Aug 2012 11:57

Thank you for clarification, we have reproduced the issue. We will inform you when it is fixed.

StanislavK
Devart Team
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Re: Retrieving Objects from the Identity Cache

Post by StanislavK » Wed 29 Aug 2012 16:12

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.

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Retrieving Objects from the Identity Cache

Post by MariiaI » Fri 07 Sep 2012 06:25

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

Post Reply