No database provider has been configured for this DbContext EF Core 3.0

Viewed 194

When I attempt to add my first migration to the project I get the following 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

What I am trying to achieve is to have my code that takes care of the DB in a .NET Core 3.0 class library separated from my web project.

So basically my file system looks like this:

Solution 
-MyProject.Data
-MyProject.Domain
-MyProject.Web

To achieve this I have found a way to move my DB class from the web project using the IDesignTimeDbContextFactory and set it up in my data project. But when I try to add migrations it fails.

Most of the answers suggest to remove the parameterless constructor from my DbContext but that is not working for me since I never had it there in the first place.

Here is my code, help is much appreciated since I found no resources for such a setup.

public class ApiDbContext : DbContext
{
        public ApiDbContext(DbContextOptions<ApiDbContext> options) : base()
        {
        }

        public DbSet<AplicationUser> AplicationUsers { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            foreach (var relationsship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
            {
                relationsship.DeleteBehavior = DeleteBehavior.Restrict;
            }

            ConfigureModelBuilderForUser(modelBuilder);
        }

        private void ConfigureModelBuilderForUser(ModelBuilder modelBuilder)
        {

        }
}




public class TemporaryDbContextFactory : IDesignTimeDbContextFactory<ApiDbContext>
{
        public ApiDbContext CreateDbContext(string[] args)
        {
            var builder = new DbContextOptionsBuilder<ApiDbContext>();
            builder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=xxx;Trusted_Connection=True;MultipleActiveResultSets=true;User=xxx;Password=xxx",
                optionsBulder => optionsBulder.MigrationsAssembly(typeof(ApiDbContext).GetTypeInfo().Assembly.GetName().Name));

            return new ApiDbContext(builder.Options);
        }
}

These two classes are in my Data project, and in my Web project I have a Startup class:

public void ConfigureServices(IServiceCollection services)
{
            services.AddEntityFrameworkSqlServer()
                .AddDbContext<ApiDbContext>(options =>
                    options.UseSqlServer(
                        Configuration.GetConnectionString("ApiDbContext"),
                        options => options.MigrationsAssembly("MyProject.Data")));

            services.AddIdentity<AplicationUser, IdentityRole>();

            services.AddControllersWithViews();

            // In production, the React files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/build";
            });
            
}
0 Answers
Related