Somebody wrote me the following question in my Entity Framework Core course:
Hi Felipe, during the course I had a question.
Prior to this course I was seeing another ASP.NET Core where they use EF Core and to save the data they used the context.SaveChangesAsync () function; Which makes me wonder, what is the difference between SaveChanges and SaveChangesAsync?
Here my answer:
The difference is that one is synchronous and the other is asynchronous. In general, methods that end in Async are asynchronous. Everything is the same in both in the sense of use and effect, the only difference is that one is synchronous and the other asynchronous.
In case you don’t know what “asynchronous” is, let’s look at an analogy. Suppose you order a pizza through your phone to have it delivered to your home. They tell you it will take 30 minutes. What do you do in those 30 minutes? Do you freeze right there, waiting for the pizza? Or do you do other chores at home while the pizza arrives? It is obvious that you want to make the most out of your time, so you decide to do some things while waiting for the pizza. Common sense.
The same goes for asynchronous programming: While the operation is performed in the database, your software is capable of doing other things in the meantime. Then, when the response comes, your software can process it.
In general, asynchronous programming makes your software more efficient when it comes to using resources. In web environments this is very important to be able to serve the requests of many users with the least amount of resources. In desktop/mobile environments, asynchronous programming allows us to perform operations without blocking the UI thread.
Asynchronous programming does not make your software run faster. It is not about speed, but about the maximum use of the resources to carry out the pending tasks.
But as I said, the use of Entity Framework Core (methods, parameters, techniques, configuration) it is all the same in general.
Regards!