I am migrating queries from Linq-to-Sql to EF. To make disabling tracking harder to forget, in Linq-To-Sql I wrote a ConfigureForQuery wrapper function which simply sets ObjectTrackingEnabled to false.
I'd like to continue to use a similar strategy, but based on the information I gathered so far, a global setting for No Tracking is not available. I'd like your opinion on how to best fix my existing queries. Some code:
public class A {
public int P1 { get; set; }
public int P2 { get; set; }
}
public class B {
public int P3 { get; set; }
public int P4 { get; set; }
}
public class MyDbContext : DbContext {
public IDbSet<A> As { get; set; }
public IDbSet<B> Bs { get; set; }
}
// scenario 1: returning IQueryable of T where IDbSet<T> is defined in a DbContext
public IQueryable<A> Get()
{
var query =
from a in db.As
join b in db.Bs
on a.P1 equals b.P3
where b.P4 > 50
select a;
return query;
}
Q1: where and how do I apply AsNoTracking?
Q2: In this case, do I need to apply AsNoTracking to Bs?
// scenario 2: returning IQueryable of T where IDbSet<T> is NOT defined in a DbContext
public class C {
public int P1 { get; set; }
public int P4 { get; set; }
}
public IQueryable<C> Get()
{
var query =
from a in db.As
join b in db.Bs
on a.P1 equals b.P3
where b.P4 > 50
select C { P1 = a.P1, P4 = b.P4};
return query;
}
Q3: Do I need AsNoTracking in this case?
Thanks for your help.