Perhaps I am misunderstanding the caching that DbContext and DbSet does but I was under the impression that there was some caching that would go on
Sometimes I use my extension method:
using System.Linq;
using System.Linq.Expressions;
namespace System.Data.Entity
{
public static class DbSetExtensions
{
public static TEntity FirstOrDefaultCache(this DbSet queryable, Expression> condition)
where TEntity : class
{
return queryable
.Local.FirstOrDefault(condition.Compile()) // find in local cache
?? queryable.FirstOrDefault(condition); // if local cache returns null check the db
}
}
}
Usage:
db.Invoices.FirstOrDefaultCache(x => x.CustomerName == "Some name");
You can replace FirstOrDefault with SingleOrDetfault also.