我正在將查詢從Linq-to-sql遷移到EF。爲了使禁用跟蹤更難以忘記,在Linq-To-Sql中,我編寫了一個ConfigureForQuery包裝器函數,它簡單地將ObjectTrackingEnabled設置爲false。
我想繼續使用類似的策略,但根據我目前收集的信息,沒有跟蹤的全局設置不可用。我想就如何最好地解決我現有的問題提出意見。部分代碼:應該在哪裏應用AsNoTracking?
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;
}
問題1:在哪裏以及如何應用AsNoTracking?
Q2:在這種情況下,我是否需要將AsNoTracking應用於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;
}
問題3:在這種情況下,我需要使用AsNoTracking嗎?
感謝您的幫助。
看到這個:http://stackoverflow.com/questions/12726878/global-setting-for-asnotracking – 2014-10-22 11:26:46
@AlaaMasoud我編輯的問題。謝謝。 – 2014-10-22 13:49:32