Code inside if statement executes while condition is false (Asp.Net MVC, C#)

Code inside if statement executes while condition is false (Asp.Net MVC, C#)

The problem

I was supposed to to add some new functionalities to that application that am supporting. Very good program, very well written in my point of view. One of the new functionalities was about some bulk operations. This mean obviously if the application was working fine, I can just use use some of the corresponding atomic operations and put them in some kind of loop.

Really hoped it was that easy. Wrote my code properly, then execute.. Something happened. I spent a whole night trying to figure out the reason why in my code one if statement condition was not met but but the instructions inside was still executing.

The investigation

I tried to debug the program and clearly the condition was false but was still getting in.

Error1.jpg

First thing I thought about was maybe another thread was accessing that same variable. So what about using a Mutex object or lock instruction in c# to make sure only on thread was accessing that variable at the time? I identified part of my program that supposed to be in the critical section, and did my change but problem still persisted. If its not concurrency, it is what?

That condition surveyQuestionDetails.QuestionId == 0 that I was expecting to be true, was always false and the if condition was executing anyway. The worse thing is that field surveyQuestionDetails.QuestionId was set to 1 explicitly prior to pass it to the service as shown in the code snippet below.

SurveyQuestionDetails surveyQuestionDetails = new SurveyQuestionDetails
 {
        SurveyId = SurveyId,
        SurveySectionId = section.Value,
        QuestionId = 1,
        QuestionTypeId = QuestionTypeDefinitions.GetValue(questionType)
 };

var savesurvey = surveyservice.SaveSurveyQuestion(surveyQuestionDetails);

The fix (or is it?)

What about Entity framework? Could this be related to it? This I doubted but I followed that instinct anyway. There are many services that are accessing the database and I realized that each of them have to create a their own database context. So, a proper repository pattern was not implemented but I still thought that the problem was not be there.

So, while I couldn't figure out what was the issue, I decided to clean the code a bit and make sure that all the services used the same database context as this is consider best practice. I didn't feel like rewriting the whole services to use a repository pattern and then use dependency injection (which obviously I will have to do later). So I make sure that whenever am using a controller I instantiate a single database context and pass it to any service that am using.

Et Voila.. I ran the application and all was working fine. But till now am wondering what database context has to do with a simple variable assignment, why that condition was false and still getting into the if statement?

The application is working fine in production actually and am not touching it for now. :-)