0

I have an asp.net core api and I am seeding data in my database, however I am using the usermanager from identity core which requires async.

I tried to async the void Main(string[] args) but that did not work.

  public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {

               var dbContext = services.GetService<ApplicationDbContext>();

                var userManager = services.GetService<UserManager<Employee>>();
                DbSeeder.CreateDefaultCompany(dbContext, userManager);

            }

            host.Run();
        }

The only way I figured out how to get around this(though, from what I read it might not be best practice is)

// inside CreateDefaultCompany

 userManager.CreateAsync(employee1, "parts").GetAwaiter().GetResult();
chobo2
  • 83,322
  • 195
  • 530
  • 832
  • You should be able to declare Main as `static async Task Main(string[] args)` as per this answer: https://stackoverflow.com/a/9212343/7034621 – orhtej2 Jul 17 '18 at 21:08
  • I did try that originally but I get: Error CS5001 Program does not contain a static 'Main' method suitable for an entry point – chobo2 Jul 17 '18 at 21:11
  • @chobo2 did you change it to Task as well? – johnny 5 Jul 17 '18 at 21:12
  • Make sure you set the project properties to ensure you are using at least C# v7.1 to get async main methods. – DavidG Jul 17 '18 at 21:14
  • You can use `async Task Main()` if you are using c# 7.1 and Visual Studio 15.3+, see [this answer](https://stackoverflow.com/a/44254451/1260204) in the marked duplicate. If you are not able to use this version then see any of the other answers in the marked duplicate. – Igor Jul 17 '18 at 21:15

0 Answers0