Registering context in services (.NET Core DI)
The warning displayed in the OnConfiguring(DbContextOptionsBuilder optionsBuilder)
method needs to be addressed. So let's remove that method (highlighted in the following code) and perform configuration inside the Startup.cs
file using dependency injection.
Refactoring the OnConfiguring() method
If we recap on how we have configured the database context, the auto-generated code had a hardcoded connection string used for configuration. To avoid it, we should have a mechanism to pass on the database context options to the DbContext
base class; let's see how to do it:
public partial class MasteringEFCoreDbFirstContext : DbContext { public virtual DbSet<Blog> Blog { get; set; } public virtual DbSet<Post> Post { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { // Move this connection string to config file later optionsBuilder.UseSqlServer(@"Server...