0

I am attempting to set up an EntityFrameworkCore.DbContext in a new project. My DbContext will live in a class library referenced by my AspNetCore Web Api project. I'm trying to register the context as a service in the ConfigureServices method of Startup.cs, as follows

services.AddDbContext<DataContext>(options => options.UseMySql(Configuration.GetConnectionString("MySql")));

I have verified in appsettings.json file, my connection string is under ConnectionStrings.MySql. The constructor for my DbContext that should be used and receiving the options is here

public DataContext(DbContextOptions<DataContext> options) : base(options) { }

However, if I try to run dotnet ef migrations add Initial, the build fails, citing a required parameterless constructor. If I add a parameterless constructor and try to add migrations again, I instead get this error

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

Since I definitely have a constructor that accepts DbContextOptions and passes it to the base constructor, I am left with configuring it via the OnConfiguring method, like

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
        optionsBuilder.UseMySql(connectionString);
    }
}

But now the problem becomes that I need the connection string from the Web Api project's app settings.json file to be passed to the class library where the DbContext lives. I have tried referencing similar questions here, but have been unsuccessful in finding and implementing solution. The accepted answer here and the top upvoted answer here looked very promising, but I was unable to figure out how to adapt them to my situation.

wizloc
  • 2,202
  • 4
  • 28
  • 54
  • Does your entry point have a `CreateHostBuilder` static method, so that cmd line tools can discover your setup? Where do you want your migrations `options.UseMySql( conf, o => o.MigrationsAssembly(...))`? – Jeremy Lakeman Oct 23 '20 at 01:26
  • have you tried using `--startup-project` with your webapi project that contains the configuration? – ESG Oct 23 '20 at 01:27
  • @JeremyLakeman Yes, I have the static CreateHostBuilder method in my Program.cs file. My migrations should be in the class library project. I have the Web Api project set as the startup project, and my class library is targeting .Net Core 3.1, which should allow me to run the command without using the -s flag from what I understand (but it still gives the same error if I use the --startup-project flag) – wizloc Oct 23 '20 at 01:39
  • Wait "the build fails, citing a required parameterless constructor"? You're directly creating the context somewhere instead of using DI? – Jeremy Lakeman Oct 23 '20 at 01:48
  • No. I am only using the DI AddDbContext method – wizloc Oct 23 '20 at 01:53

0 Answers0