A common mistake almost all the developer does in the Entity Framework

Vishal Pathak
Abhima C# Programming
3 min readApr 17, 2022

As you are already knowing that Entity Framework is an ORM framework which is used for CRUD operations in ASP,NET Applications.

Today we are going understand the very common mistake done by almost all the developer.

Magnificent Sri Krishna Mandir in Durbar Square at Patan Nepal

First we will discuss the problem that is being faced by us and then will show you the solution for that, let’s start with the first problem.

External update to the database is not reflecting in the already running Application :- So the problem here is if we are retrieving any data from the database after external application has updated the data in the database. The Entity Framework will not give the latest data. And if you restart the application then only it will reflect. So let’s see the example below.

Example:-

So in the above example we have created a asp.net core worker process which is having a count object which will be after each execution. If the value of the count object is 0 then it will insert one row in student table. And if the count value is not 0 then it will retrieve the data and print the name of the student. After running this we will update the table from the MSSQL server and will wait for some time, if it is printing the update student name or not.

Output:-

After running the application one row got inserted as given below in the Screenshot.

Table data before changing the value

Application is still running and now we have changed the student name from Vishal Pathak to just Vishal.

Data got updated externally

But in the below output it is visible that application is still printing the same name as Vishal Pathak.

Output

So to avoid this issue we have one method in Entity Framework called AsNoTracking. We already have article for AsNoTracking method below.

To resolve this issue we have updated the above code. So here we are just adding AsNoTracking Method.

As you can see on line number 42 we have added AsNoTracking method. Now We will run the application again. We followed the same steps and now application started printing the value immediately after updating the value.

Output with changed value

As you can see that in the above output system started printing the updated name as soon as we changed it into the Database.

Thank you for reading please comment your suggestions, share the article, follow me and Abhima C# Programming publication.

Bhagavad Gita Verse of the Day

सर्वत: पाणिपादं तत्सर्वतोऽक्षिशिरोमुखम् |
सर्वत: श्रुतिमल्लोके सर्वमावृत्य तिष्ठति || 14||

sarvataḥ pāṇi-pādaṁ tat sarvato ’kṣhi-śhiro-mukham
sarvataḥ śhrutimal loke sarvam āvṛitya tiṣhṭhati

BG 13.14: Everywhere are His hands and feet, eyes, heads, and faces. His ears too are in all places, for He pervades everything in the universe.

--

--

Vishal Pathak
Abhima C# Programming

love ❤ coding, solving some industry problems technologies: JavaScript, C#, Angular, PLSQL, Docker Want to learn: Python, Go language, AI, ML and Cloud