Aspnetcore.docs: Says Asynchronous Code should be used but DbInitializer.cs Example is Not Asynchronous

Created on 22 Aug 2018  Â·  5Comments  Â·  Source: dotnet/AspNetCore.Docs

The documentation says to always use asynchronous code yet the code given in DbInitializer.cs is synchronous.

The code should be re-written to use asynchronous methods (unless I am missing something).

This would involve changing commands from:

context.Student.Add(s);
context.SaveChanges();

To

await context.Student.AddAsync(s);
await context.SaveChangesAsync();

Either seems to work ok though and produce the desired result. Maybe as it only runs once during project startup it doesn't really matter?


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

P2 Source - Docs.ms

Most helpful comment

Ok thanks Rick. I can see why it isn't needed there in that case.
Robin

All 5 comments

@robinwilson16 good suggestion. We hope to rewrite the initializer . See #8138

@robinwilson16 given seeding the DB is a one-time operation for non-production code, simplicity/debugabilty is more important. I've had to debug seed code more than once.

Ok thanks Rick. I can see why it isn't needed there in that case.
Robin

Thanks for bringing this up. When we update the doc we'll explain why we're using synchronous.

The DB initializer is only used early in project development, not in production code. It's OK to use Sync in this case since best perf is not a concern.

Was this page helpful?
0 / 5 - 0 ratings