0

NOTE: I'm experimenting with EntityFrameworkCore 5.x and .NET 6.0 inside of VS2022 Preview.

I've got my code set up to loop and add a first name and a last name to a user object which updates a user table in SQLServer. If I have the loop, it updates the database but short circuits the loop. Removing the loop adds a single user to the database and exits the program as expected.

I understand it's best practice to do all the adds and then update, but what is the technical reason for the short circuit?

The code:

using EFGetStarted;
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            EFGetStartedEntryPoint.Execute();
            Console.ReadKey();
        }
    }
}
using EFGetStarted.Controllers;
using EFGetStarted.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFGetStarted
{
    public static class EFGetStartedEntryPoint
    {
        private static string connstring = @"Server=myServer;Database=EFGetStarted;Trusted_Connection=true";

        public async static void Execute()
        {
            var contextOptions = new DbContextOptionsBuilder<EFGetStartedDbContext>()
                .UseSqlServer(connstring)
                .Options;

            using (var db = new EFGetStartedDbContext(contextOptions))
            {
                int counter = 1;
                while (true)
                {
                    User user = new User() { FirstName = "John", LastName = "Doe" + counter.ToString() };
                    await db.AddAsync<User>(user);
                    await db.SaveChangesAsync();
                    Console.WriteLine("SaveChanges Completed Successfully");
                    counter++;
                    Console.ReadKey();
                }
            }
        }
    }
}
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFGetStarted.Models
{
    public class User
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Int64 UserID { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFGetStarted.Models
{
    public class EFGetStartedDbContext : DbContext
    {
        public DbSet<User> tbUsers { get; set; }
        public EFGetStartedDbContext(DbContextOptions<EFGetStartedDbContext> options)
            : base(options) 
        {
        }
    }
}
  • 2
    Without a proper [mcve], it's impossible to provide specific advice. However, the issue is almost certainly related to the fact that your `Execute()` method has `void` as the return type. It's perfectly normal for the method to return at the first `await`; it's the job of the caller to await the async operation, but your code doesn't allow that because the `Execute()` method returns `void` instead of `Task`, and so the program exits before the loop can finish processing. See duplicate. – Peter Duniho Jul 02 '21 at 00:05
  • @PeterDuniho That's fair. I've updated with the reproducible code. I'll take a look at the void Q&A StackOverflow suggested. Thanks for taking the time to comment :) – Blaine Harris Jul 02 '21 at 00:14
  • 1
    See also https://stackoverflow.com/questions/16712172/an-entry-point-cannot-be-marked-with-the-async-modifier. Your `Main()` method should be `async`, so you can `await` the `Execute()` method (once it returns `Task` of course). See also https://stackoverflow.com/questions/13002507/how-can-i-call-an-async-method-in-main and https://stackoverflow.com/questions/9208921/cant-specify-the-async-modifier-on-the-main-method-of-a-console-app and, of course, https://stackoverflow.com/search?q=%5Bc%23%5D+async+main, paying close attention to updates since async `Main()` methods were made legal. – Peter Duniho Jul 02 '21 at 00:17
  • @PeterDuniho Those links helped fix the problem. I'll try wrapping my head around "the why" tomorrow. Thanks! – Blaine Harris Jul 02 '21 at 00:33

0 Answers0