Skip to content

Commit 653e335

Browse files
committed
EF AsNoTracking
1 parent 34c910b commit 653e335

7 files changed

Lines changed: 66 additions & 2 deletions

File tree

SharpRepository.EfCoreRepository/EfCoreCompoundKeyRepositoryBase.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = nul
7070
{
7171
var query = DbSet.AsQueryable();
7272

73+
if (fetchStrategy.NoTracking)
74+
{
75+
query = query.AsNoTracking();
76+
}
77+
7378
return fetchStrategy == null ? query : fetchStrategy.IncludePaths.Aggregate(query, (current, path) => current.Include(path));
7479
}
7580

@@ -159,6 +164,11 @@ protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = nul
159164
{
160165
var query = DbSet.AsQueryable();
161166

167+
if (fetchStrategy.NoTracking)
168+
{
169+
query = query.AsNoTracking();
170+
}
171+
162172
return fetchStrategy == null ? query : fetchStrategy.IncludePaths.Aggregate(query, (current, path) => current.Include(path));
163173
}
164174

SharpRepository.EfCoreRepository/EfCoreRepositoryBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = nul
8888
{
8989
var query = DbSet.AsQueryable();
9090

91+
if (fetchStrategy.NoTracking)
92+
{
93+
query = query.AsNoTracking();
94+
}
95+
9196
return fetchStrategy == null ? query : fetchStrategy.IncludePaths.Aggregate(query, (current, path) => current.Include(path));
9297
}
9398

SharpRepository.EfRepository/EfCompoundKeyRepositoryBase.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ protected override void SaveChanges()
7070
protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null)
7171
{
7272
var query = DbSet.AsQueryable();
73+
74+
if (fetchStrategy.NoTracking)
75+
{
76+
query = query.AsNoTracking();
77+
}
78+
7379
return fetchStrategy == null ? query : fetchStrategy.IncludePaths.Aggregate(query, (current, path) => current.Include(path));
7480
}
7581

@@ -158,6 +164,12 @@ protected override void SaveChanges()
158164
protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null)
159165
{
160166
var query = DbSet.AsQueryable();
167+
168+
if (fetchStrategy.NoTracking)
169+
{
170+
query = query.AsNoTracking();
171+
}
172+
161173
return fetchStrategy == null ? query : fetchStrategy.IncludePaths.Aggregate(query, (current, path) => current.Include(path));
162174
}
163175

@@ -247,6 +259,12 @@ protected override void SaveChanges()
247259
protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null)
248260
{
249261
var query = DbSet.AsQueryable();
262+
263+
if (fetchStrategy.NoTracking)
264+
{
265+
query = query.AsNoTracking();
266+
}
267+
250268
return fetchStrategy == null ? query : fetchStrategy.IncludePaths.Aggregate(query, (current, path) => current.Include(path));
251269
}
252270

SharpRepository.EfRepository/EfRepositoryBase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ protected override void SaveChanges()
101101
protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null)
102102
{
103103
var query = DbSet.AsQueryable();
104+
105+
if (fetchStrategy.NoTracking)
106+
{
107+
query = query.AsNoTracking();
108+
}
109+
104110
return fetchStrategy == null ? query : fetchStrategy.IncludePaths.Aggregate(query, (current, path) => current.Include(path));
105111
}
106112

SharpRepository.Repository/FetchStrategies/AbstractFetchStrategy.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ namespace SharpRepository.Repository.FetchStrategies
77
public abstract class AbstractFetchStrategy<T> : IFetchStrategy<T>
88
{
99
public abstract IEnumerable<string> IncludePaths { get; }
10+
11+
public abstract bool NoTracking { get; }
12+
13+
public abstract IFetchStrategy<T> AsNoTracking();
14+
1015
public abstract IFetchStrategy<T> Include(Expression<Func<T, object>> path);
1116
public abstract IFetchStrategy<T> Include(string path);
1217

1318
public override string ToString()
1419
{
15-
return String.Format("Type: {0} Includes: {1}",
20+
return string.Format("Type: {0} Includes: {1}",
1621
GetType().Name,
17-
String.Join(",", IncludePaths)
22+
string.Join(",", IncludePaths)
1823
);
1924
}
2025
}

SharpRepository.Repository/FetchStrategies/GenericFetchStrategy.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,33 @@ namespace SharpRepository.Repository.FetchStrategies
1212
public class GenericFetchStrategy<T> : AbstractFetchStrategy<T>
1313
{
1414
private readonly IList<string> _properties;
15+
private bool _noTracking;
1516

1617
public GenericFetchStrategy()
1718
{
1819
_properties = new List<string>();
20+
_noTracking = false;
1921
}
2022

2123
public override IEnumerable<string> IncludePaths
2224
{
2325
get { return _properties; }
2426
}
2527

28+
public override bool NoTracking
29+
{
30+
get { return _noTracking; }
31+
}
32+
33+
34+
35+
public override IFetchStrategy<T> AsNoTracking()
36+
{
37+
_noTracking = true;
38+
39+
return this;
40+
}
41+
2642
public override IFetchStrategy<T> Include(Expression<Func<T, object>> path)
2743
{
2844
return Include(path.ToIncludeString());

SharpRepository.Repository/FetchStrategies/IFetchStrategy.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ public interface IFetchStrategy<T>
1313
{
1414
IEnumerable<string> IncludePaths { get; }
1515

16+
bool NoTracking { get; }
17+
1618
IFetchStrategy<T> Include(Expression<Func<T, object>> path);
1719

1820
IFetchStrategy<T> Include(string path);
21+
22+
IFetchStrategy<T> AsNoTracking();
1923
}
2024
}

0 commit comments

Comments
 (0)