0

I just started with EF Core and noticed that they have got all kind of XXXAsync methods now. I started using them and hit the rock with the use of context.SaveChnagesAsync() while doing simple basic insert operation. It went like this :

       public async void SaveUserAsync(User user)
        {
            using (var context = GetContextTransient())
            {
                Model.User contextUser = new Model.User
                {
                    EmailId = user.EmailId,
                    UserName = user.UserName,
                    JoinedDateTime = DateTime.Now
                };

                await context.User.AddAsync(contextUser).ConfigureAwait(false);
                await context.SaveChangesAsync(true).ConfigureAwait(false);

            }
        }

The above implementation was not inserting any records to the DB but doing simple change as below, then it worked :

        public async Task<int> SaveUserAsync(User user)
        {
            using (var context = GetContextTransient())
            {
                Model.User contextUser = new Model.User
                {
                    EmailId = user.EmailId,
                    UserName = user.UserName,
                    JoinedDateTime = DateTime.Now
                };

                await context.User.AddAsync(contextUser).ConfigureAwait(false);
                int result = await context.SaveChangesAsync(true).ConfigureAwait(false);
                return result;
            }
        }

Now I know doing aysnc - await with void return type is not recommended but still shouldn't 1st implementation work because I am doing await on context.SaveChangesAsync() ? Is my understanding of async- await correct or am I missing something?

steamrolla
  • 2,373
  • 1
  • 29
  • 39
Vishal Anand
  • 1,431
  • 1
  • 15
  • 32
  • 1
    As I know, is not recommended to use async with void, it's better to use Task or in your case Task, so you're right about your conclussion – H. Herzl Mar 13 '17 at 04:19
  • 2
    maybe it throws but you cannot observe the exception because you are not returning Task? Maybe your application exits before the task started to run and you are not able to await your call because you don't return Task? Just return Task and await the call. `async void` should be basically only used for events. – Pawel Mar 13 '17 at 04:46
  • Very unclear what you are trying to achieve. Your code (async fire-and-forget) is clearly shows that you *do not care about result*, but than you complain that method does not do something... Which one is you goal? – Alexei Levenkov Mar 13 '17 at 05:03
  • @AlexeiLevenkov: Yes its a fire and forget. So fire and forget methods will not have any effect? Shouldn't record be inserted into DB if main application continues to run and not die? – Vishal Anand Mar 13 '17 at 05:09
  • 1
    It should work the same (with respect to inserting record into database) with whatever return type. – Evk Mar 13 '17 at 06:02
  • 2
    We need more context - who is calling this, how and why. The answer will be different for a WinForms/WPF event or a WebAPI call. – H H Mar 13 '17 at 13:33
  • @VishalAnand *"Shouldn't record be inserted into DB..."* So long as there was no exception, yes. But are you sure your application did continue to run? Perhaps If this is a console program and `SaveUserAsync` is the last line of the program it is very likely that the program will have closed before the save every had a chance to start. "Fire and forget" means "I don't care if the process does not ever get started". You appear to care if the process started (and completed) because you are relying on that database entry. Therefor ***you can't do Fire and Forget.*** – Scott Chamberlain Mar 13 '17 at 17:39
  • @HenkHolterman It is getting called from a console application. – Vishal Anand Mar 14 '17 at 06:14
  • @ScottChamberlain: I thought exactly the same and modified the code as shown and it works. I am using console application for executing the code and the application does not die because there is a Console.ReadKey() in the end. Maybe I should just wait longer. – Vishal Anand Mar 14 '17 at 06:18
  • The you need to return a Task and ccall .Result in the top method. But that won't transfer to other app types, using Result or Wait is usually not acceptable. – H H Mar 14 '17 at 09:47
  • check this http://stackoverflow.com/questions/30042791/entity-framework-savechanges-vs-savechangesasync-and-find-vs-findasync – Mohammad Akbari Mar 15 '17 at 04:38

0 Answers0