_context.SaveChanges is not working

Question: Hello ErnesTech, why is _context.SaveChanges() not working in Entity #Framework C# code? The code execution goes well without an error but then the data is not saved into the Database. What is going on?



Please Login to see the rest of the answer

Answer: One of the reasons that makes Entity Framework not to save changes to the database is when you retrieve the Entity or record from the Database when using AsNoTracking(). Remember that AsNoTracking will not track the changes of that specific Record or Entity when it is changed.

Nevertheless, even when the EF Core Engine at it's slightest detects the changes done to the Record or Entity the computation/tracking becomes expensive and also factor in the work done on other Threads as well (in regard to Async Await Functions). 

If you are using Async Await in your function and you retrieve the Database Record using the AsNoTracking() then afterwards you try to make modifications to the Record then attempt to save changes, this might not work.


1. Make sure that if you intend to make changes to the Record that you retrieve from the database, DO NOT USE AsNoTracking(). The AsNoTracking() is used when you intend to display the record (It was meant for ReadOnly Records).

2. If you are using Async Await in your function, make sure that you commit to awaiting all necessary operation either #Database bound or Input Output (IO) bound. This can be overridden if you do not need to Await operation simply Fire and Forgert using a Discard directive (_ = MyFunction).

3. DO NOT mix Async Calls with None Async Calls, if the function utilizes Async Await, make sure all the calls to other functions are Awaitable.

I hope this helps you, if it does make sure to leave a comment below to help others resolve thier errors.

[Important]: Offcause things do not always work, when you find yourself in a situetion where EF Core #SaveChanges instead just working for you, there could other things in play here. If you could have a solution to pivot do so. For Example, I encountered the same sitietion where hours were spent trying to troubleshoot why the saved Entity ID was getting overridden by the existing ID in newly created Object.

The idea was to:

1. Create an Object and Save in the Database
2. Use the Object ID created in step 1, in the second Created Object. This way Object 2 is referencing Object 1 ID (Like Foreign Key without going through the headaches of creating Navition in EF Core).
3. This was failing, everytime I save an Object in Step 1, Step 2 does not have Step 1 Object ID

[Update]: I was able to resolve this issue, the problem was that the error was getting thrown in another function that added a another Object to the database. 
                  When an Exception occured the Database Transaction was getting roled back since EF Core commit to the database when there are no errors.
                   This issue drove me crazy and could not know what was going on. Thanks much!

Thank you!


Write an Article

_context.SaveChanges is not working

Edited Version 2

Question: Hello ErnesTech, why is _context.SaveChanges() not working in Entity Framework C# code? The code execution goes well without an error but then the data is not saved into the Database. What is going on?


Answer: One of the reasons that makes Entity Framework not to save changes to the database is when you retrieve the Entity or record from the Database when using AsNoTracking(). Remember that AsNoTracking will not track the changes of that specific Record or Entity when it is changed.

Nevertheless, even when the EF Core Engine at it's slightest detects the changes done to the Record or Entity the computation/tracking becomes expensive and also factor in the workd done on other Threads as well (in regard to Async Await Functions). 

If you are using Async Await in your function and you retrieve the Database Record using the AsNoTracking() then afterwards you try to make modifications to the Record then attempt to save changes, this might not work.

Verify the Steps Below:

1. Make sure that if you intend to make changes to the Record that you retrieve from the database, DO NOT USE AsNoTracking(). The AsNoTracking() is used when you intend to display the record (It was meant for ReadOnly Records).

2. If you are using Async Await in your function, make sure that you commit to Awaiting all necessary operation either Database bound or Input Output (IO) bound. This can be overridden if you do not need to Await operation simply Fire and Forgert using a Discard directive (_ = MyFunction()).

3. DO NOT mix Async Calls with None Async Calls, if the function utilizes Async Await, make sure all the calls to other functions are Awaitable.

I hope this helps you, if it does make sure to leave a comment below to help others resolve thier errors.

Thank you!

If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.


© 2022 - ErnesTech - Privacy