I am implementing method for seeding data (and calling it in Startup.Configure() method) which looks like
if(!_context.Table.Any())
{
_context.Table.Add();
_context.SaveChanges();
}
And I want to make it async using Task, so I changed my code to this
Task.Run(async () =>
{
if(! await _context.Table.AnyAsync())
{
await _context.Table.AddAsync();
await _context.SaveChangesAsync();
}
});
But It didn't work. So I add Wait()
to the Task.Run()
and method started working.
Task.Run(async () =>
{
if(! await _context.Table.AnyAsync())
{
await _context.Table.AddAsync();
await _context.SaveChangesAsync();
}
}).Wait();
So, are there any differences between first and last parts of code?
As I know, Wait()
method blocks the thread, so are these parts of code the same?