1. Firstly, you should not create a context inside the controller, avoid using 'new' with dependencies, because it will make your code uncheckable, in my case when I use UnitOfWork, I inject it as an instance of IUnitOfWork, that is, really , the extension is MyConext, and you have to inject it into the StartUp class ... For this, I have a private method (for doing this in one private call) that looks like this:
private void AddEntityFrameworkAndDbContext(IServiceCollection services) { services.AddEntityFrameworkSqlServer(); var migrationsAssemblyName = typeof(MyContext).GetTypeInfo().Assembly.GetName().Name; services.AddDbContext<MyContext>(options => { options.UseSqlServer( "MY CONNECTION STRING GOES HERE (BUT I RETREIVE IT FROM ANOTHER SERVICE)", sqlServerOptionsAction: sqlOptions => { sqlOptions.MigrationsAssembly(migrationsAssemblyName); sqlOptions.EnableRetryOnFailure(maxRetryCount: 5, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null); }); }, ServiceLifetime.Scoped
I call this private method from ConfigureServices (IServiceCollection services), as I said, in the StartUp class
// Add EF, and UoW AddEntityFrameworkAndDbContext(services);
2.- Secondly ( but I would say that this is your real problem ) I would say that you missed the base. Setting (options); in your context, it should look like this:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlServer(@"Server=(local);Database=VotingAppDB;User ID=sa;Password=123;"); } base.OnConfiguring(optionsBuilder); }
Also, please take a look at the answer I wrote a few weeks ago: How to configure EF6 Migrations with ASP.NET Core
Also, this UnitOfWork project is worth reading, see it here: https://github.com/arch/UnitOfWork
I hope this helps,
Juan