0

I have two objects, when I call member functions to retrieve data from the database, order or calling these functions effecting the results.

Whichever function I call the second time, it is returning extra data:

IQueryable<SameClass> object_1;
IList<SameClass> object_2;

I'm calling functions like this:

object_1= this.sameService.function1();
object_2= this.sameService.function2();

And finally in these functions, I'm calling same repository for data access:

IQueryable<SameClass> object_3= this.sameRepo.GetMany(whr => data checks).AsNoTracking();

IList<SameClass> object_3 = this.sameRepo.GetAll().ToList();

For example, if I call object_2 = this.sameService.function2() after object_1= this.sameService.function1(), it returns 51 records, but when I call it first, it returns 34 records which is the correct response.

I've tried adding an IDisposable to SameClass and when I create IQueryable<SameClass>, used it with try{} finally{} blocks where I dispose the object, and added .AsNoTracking() but results are still the same.

What am I missing?

MRC_
  • 1
  • 2
  • Obviously the code you have provided should work, so the problem is in the code you haven't provided. I can't think of any way to troubleshoot code I can't see. I would suggest however that you figure out if there is some pattern to those 17 extra records you're getting. – John Wu Apr 01 '23 at 06:36
  • Thanks for your answer. Normally these two functions are completely working, I even use them in other pages of my application. But when I call them in same controller's same function, I've got this problem. I believe there is some query caching or global variable holds some data. I'm searching a solution like 'you should clear your query cache like this' or 'you should check your base class to have this enabled'. – MRC_ Apr 01 '23 at 07:09
  • `I believe there is some query caching` - on contrary, it's that the IQueryable version is [not executed](https://stackoverflow.com/q/7324033/11683) in the first place at the point of `.AsNoTracking()`. – GSerg Apr 01 '23 at 07:43
  • My bad, originally code doesn't have .AsNoTracking(). I've added it recently with hoping it will solve my problem. – MRC_ Apr 01 '23 at 07:57
  • That does not change anything. – GSerg Apr 01 '23 at 07:58
  • Have you tried using context.ChangeTracker.Clear() to see if there is any tracked entries already in the tracker before making the second call? – Bingla Apr 01 '23 at 08:08
  • Try adding a profiler in your database and check: 1 - if the query is executed or not in both cases to confirm or discard a cache issue; 2- the actual sql query executed to check if there are differences in the query. Try to execute them directly in your SQL Manager and examine the results. – Alpha75 Apr 01 '23 at 08:13
  • 2
    This severely lacks an [mre]. – Guru Stron Apr 01 '23 at 08:14
  • Forget about what order the calls come in. The bottom line is *you have a function that returns the wrong results* sometimes. Obviously that function has a bug, even if it works okay under most conditions. Can you share the actual code? – John Wu Apr 01 '23 at 08:53
  • @JohnWu, If I change the order of calling, always the second called function returns wrong data. Unfortunately I can't share actual code. – MRC_ Apr 01 '23 at 09:07
  • @GuruStron, I will try to simplify services and update my question until tomorrow evening. – MRC_ Apr 01 '23 at 09:16
  • GSerg and Alpha75, thanks for your advices I will try them and update here. – MRC_ Apr 01 '23 at 09:16

0 Answers0