The codebase I work on uses Entity Framework and the repository pattern. In order to get decent performance (i.e., 150 ms database calls instead of 1500 ms calls), I've found I need to use AsNoTracking when selecting data to avoid caching.
The following code works splendidly:
using (var context = new DeviceCloudModel())
{
var model = context.Devices
.AsNoTracking()
.Include(d => d.DeviceSettings)
.Where(d => d.SerialNumber == serialnumber && o.IsActive).FirstOrDefault();
}
However, this doesn't, and is just as slow (I'm guessing it's caching again):
var predicate = (filter ?? (x => true)).Compile(); // assume filter is always null
var model = _repository
.Get(o => o.SerialNumber == serialnumber && o.IsActive)
.Where(predicate)
.FirstOrDefault();
Here's my repository Get method:
public override List<Device> Get(Expression<Func<Device, bool>> filter = null)
{
var predicate = (filter ?? (x => true)).Compile();
var ret = _context.Devices
.AsNoTracking()
.Include(d => d.DeviceSettings)
.Where(predicate)
.ToList();
return ret;
}
We want to improve our performance, but we don't want to get rid of the repository pattern or throw an ugly hack on top of it. Is there any way to use AsNoTracking with the current repository code we have in place?