BethanysPieShop icon indicating copy to clipboard operation
BethanysPieShop copied to clipboard

Unable to Create object of type 'AppDbContext'

Open rswetnam opened this issue 7 years ago • 4 comments

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,

rswetnam avatar Sep 27 '17 14:09 rswetnam

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); } } }

djjohnson-net avatar Jan 10 '18 22:01 djjohnson-net

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.

Smitsons avatar May 26 '19 07:05 Smitsons

Here is solution.

  1. First, modify your DbInitializer.Seed Method in a way as follows

image

  1. Make this changes to Program Static void Main Method, and Migrations should work

image

shamil-sadigov avatar May 28 '19 19:05 shamil-sadigov

Thanks for the solution. Its working perfectly.

mananchoksi007 avatar May 06 '20 12:05 mananchoksi007