BethanysPieShop
BethanysPieShop copied to clipboard
Unable to Create object of type 'AppDbContext'
Hi Gill - after pointing to my local db - I get the following message when running update-database in the package maanager: Unable to create an object of type 'AppDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<AppDbContext>' to the project,
I have the same issue. Does anyone have a fix yet?
Here is the fix I used. Just add this class to the project and then use the Package Manager Console "update-database"
using BethanysPieShop.Models; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; using Microsoft.Extensions.Configuration; using System.IO;
namespace BethanysPieShop { public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<AppDbContext> { public AppDbContext CreateDbContext(string[] args) { IConfigurationRoot configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); var builder = new DbContextOptionsBuilder<AppDbContext>(); var connectionString = configuration.GetConnectionString("DefaultConnection"); builder.UseSqlServer(connectionString); return new AppDbContext(builder.Options); } } }
Unable to create an object of type 'AppDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<AppDbContext>' to the project
Gill, Any update for this error in Package Manager Console ? Please, don't be silent. I need your solution.
Here is solution.
- First, modify your DbInitializer.Seed Method in a way as follows
- Make this changes to Program Static void Main Method, and Migrations should work
Thanks for the solution. Its working perfectly.