Home > Articles > Details

_context.SaveChanges is not working


Question: 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?

Login to See the Rest of the Answer

Answer:
One of the reasons that make Entity Framework not save changes to the database is when you retrieve the Entity or records from the Database using "AsNoTracking()". When AsNoTracking() function is called on the chain of execution (Functional Programming, LINQ) will not track the changes of that specific Record or Entity.

According to Learn.Microsoft.com, the AsNoTracking() function means the returned query of entities will not be cached in the DbContext or ObjectContext. (source: learn.microsoft.com accessed 11/5/2022)

In addition, when the EF Core Engine at its slightest detects the changes done to the Record or Entity the computation of implicit tracking of the changed objects becomes expensive. In some cases, the changed workload might not be saved to the Database, and the errors are not emitted to the Output Console or logs.

Ways to Resolve the issue

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

2. If you are using Async Await in your function, make sure that you commit to awaiting all necessary operations either  Database bound or Input Output (IO) bound. This can be overridden if you do not need to Await operation simply Fire and Forget 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.

[Important]: Because things do not always work and the ever-changing APIs, when you find yourself in a situation where EF Core SaveChanges still does not work then use this logic to find the cause of the behavior.

[Anecdotal]: Same error happened to me when I was programming, below are the steps that I took to resolve the issue.

The idea was to:

1. Create an Object and Save it 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 Navigation in EF Core).

3. This was failing, every time I save an Object in Step 1, Step 2 does not have the 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 another Object to the database.

When an Exception occurred the Database Transaction was getting rolled back since EF Core commits to the database when there are no errors. This issue drove me crazy and could not know what was going on.


_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