1+ using System . Linq ;
2+ using NUnit . Framework ;
3+ using SharpRepository . EfCoreRepository ;
4+ using SharpRepository . Tests . Integration . Data ;
5+ using SharpRepository . Tests . Integration . TestObjects ;
6+ using System . Collections . Generic ;
7+ using Shouldly ;
8+ using SharpRepository . Repository . FetchStrategies ;
9+ using Microsoft . Data . Sqlite ;
10+ using Microsoft . EntityFrameworkCore ;
11+
12+ namespace SharpRepository . Tests . Integration . Spikes
13+ {
14+ [ TestFixture ]
15+ public class EfCoreAsNoTracking
16+ {
17+ private TestObjectContextCore dbContext ;
18+
19+ [ SetUp ]
20+ public void SetupRepository ( )
21+ {
22+ var dbPath = EfDataDirectoryFactory . Build ( ) ;
23+
24+ var connection = new SqliteConnection ( "DataSource=:memory:" ) ;
25+ connection . Open ( ) ;
26+
27+ var options = new DbContextOptionsBuilder < TestObjectContextCore > ( )
28+ . UseSqlite ( connection )
29+ . Options ;
30+
31+ using ( dbContext = new TestObjectContextCore ( options ) )
32+ {
33+ dbContext . Database . EnsureCreated ( ) ;
34+
35+ const int totalItems = 5 ;
36+
37+ for ( int i = 1 ; i <= totalItems ; i ++ )
38+ {
39+ dbContext . Contacts . Add (
40+ new Contact
41+ {
42+ ContactId = i . ToString ( ) ,
43+ Name = "Test User " + i ,
44+ EmailAddresses = new List < EmailAddress > {
45+ new EmailAddress {
46+ ContactId = i . ToString ( ) ,
47+ EmailAddressId = i ,
48+ Email = "omar.piani." + i . ToString ( ) + "@email.com" ,
49+ Label = "omar.piani." + i . ToString ( )
50+ }
51+ }
52+ } ) ;
53+ }
54+
55+ dbContext . SaveChanges ( ) ;
56+ }
57+
58+ // reistantiate in order to lose caches
59+ dbContext = new TestObjectContextCore ( options ) ;
60+ }
61+
62+ [ Test ]
63+ public void EfCoreGetAllSateShouldBeUnchanged ( )
64+ {
65+ var repository = new EfCoreRepository < Contact , string > ( dbContext ) ;
66+
67+ var firstContact = repository . GetAll ( ) . First ( ) ;
68+
69+ dbContext . Entry ( firstContact ) . State . ShouldBe ( EntityState . Unchanged ) ;
70+ }
71+
72+ [ Test ]
73+ public void EfCoreGetAllWithStrategySateShouldBeUnchanged ( )
74+ {
75+ var repository = new EfCoreRepository < Contact , string > ( dbContext ) ;
76+
77+ var strat = new GenericFetchStrategy < Contact > ( ) ;
78+
79+ var firstContact = repository . GetAll ( strat ) . First ( ) ;
80+
81+ dbContext . Entry ( firstContact ) . State . ShouldBe ( EntityState . Unchanged ) ;
82+ }
83+
84+ [ Test ]
85+ public void EfCoreGetAllWithStrategyAsNoTrackingSateShouldBeUnchanged ( )
86+ {
87+ var repository = new EfCoreRepository < Contact , string > ( dbContext ) ;
88+
89+ var strat = new GenericFetchStrategy < Contact > ( )
90+ . AsNoTracking ( ) ;
91+
92+ var firstContact = repository . GetAll ( strat ) . First ( ) ;
93+
94+ dbContext . Entry ( firstContact ) . State . ShouldBe ( EntityState . Detached ) ;
95+ }
96+
97+ [ Test ]
98+ public void EfCoreGetSateShouldBeUnchanged ( )
99+ {
100+ var repository = new EfCoreRepository < Contact , string > ( dbContext ) ;
101+
102+ var firstContact = repository . Get ( "1" ) ;
103+
104+ dbContext . Entry ( firstContact ) . State . ShouldBe ( EntityState . Unchanged ) ;
105+ }
106+
107+ [ Test ]
108+ public void EfCoreGetWithStrategySateShouldBeUnchanged ( )
109+ {
110+ var repository = new EfCoreRepository < Contact , string > ( dbContext ) ;
111+
112+ var strat = new GenericFetchStrategy < Contact > ( ) ;
113+
114+ var firstContact = repository . Get ( "1" , strat ) ;
115+
116+ dbContext . Entry ( firstContact ) . State . ShouldBe ( EntityState . Unchanged ) ;
117+ }
118+
119+ [ Test ]
120+ public void EfCoreGetWithStrategyAsNoTrackingSateShouldBeUnchanged ( )
121+ {
122+ var repository = new EfCoreRepository < Contact , string > ( dbContext ) ;
123+
124+ var strat = new GenericFetchStrategy < Contact > ( )
125+ . AsNoTracking ( ) ;
126+
127+ var firstContact = repository . Get ( "1" , strat ) ;
128+
129+ dbContext . Entry ( firstContact ) . State . ShouldBe ( EntityState . Detached ) ;
130+ }
131+ }
132+ }
0 commit comments