Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to Create object of type 'AppDbContext' #1

Open
rswetnam opened this issue Sep 27, 2017 · 4 comments
Open

Unable to Create object of type 'AppDbContext' #1

rswetnam opened this issue Sep 27, 2017 · 4 comments

Comments

@rswetnam
Copy link

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' to the project,

@djjohnson-net
Copy link

djjohnson-net commented Jan 10, 2018

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
{
public AppDbContext CreateDbContext(string[] args)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
var builder = new DbContextOptionsBuilder();
var connectionString = configuration.GetConnectionString("DefaultConnection");
builder.UseSqlServer(connectionString);
return new AppDbContext(builder.Options);
}
}
}

@Smitsons
Copy link

Unable to create an object of type 'AppDbContext'.
Add an implementation of 'IDesignTimeDbContextFactory' to the project

Gill, Any update for this error in Package Manager Console ?
Please, don't be silent. I need your solution.

@shamil-sadigov
Copy link

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

@mananchoksi007
Copy link

mananchoksi007 commented May 6, 2020

Thanks for the solution. Its working perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants