Kgn-web
Kgn-web

Reputation: 7555

EF Core 2.1 - Unable to create an object of type 'MyAppContext'. Add an implementation of 'IDesignTimeDbContextFactory<MyAppContext>' to the project,

I am using ASP.Net Core 2.1 with EF 2.1 This is how my Context class looks like

 public class MyAppContext
    : DbContext
{
    private string _dbConnection = @"Data Source=myServer;Database=SampleApp;uid=sa;password=******";
    public MyAppContext(DbContextOptions<MyAppContext> options)
        : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("app");
        base.OnModelCreating(modelBuilder);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(_dbConnection);
        base.OnConfiguring(optionsBuilder);
    }

    public DbSet<User> Users { get; set; }
    public DbSet<Phone> Phones { get; set; }
}

This is how my StartUp.cs & Program.cs looks like

 public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
    }
}

Program.cs

 public class Program
{

    public static void Main(string[] args)
    {
        BuildWebHost(args).Build().Run();
    }

    public static IWebHostBuilder BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();

}}

I checked below links:-

& few others.

But most of them were around when Migrating 1.x to 2.x but this is my fresh application. No migrations

How do I fix this error??

Thanks.

Upvotes: 1

Views: 4656

Answers (1)

Miamy
Miamy

Reputation: 2299

You haven't a public constructor without parameters in your database class, so you need to tell a compiler how to create an instance of your database.

Just add a new class:

    internal class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<MyAppContext>
    {
        public MyAppContext CreateDbContext(string[] args)
        {
            var builder = new DbContextOptionsBuilder<MyAppContext>();
            builder.UseSqlServer(_dbConnection);
            var context = new MyAppContext(builder.Options);
            return context;
        }
    }

Edit

Here's example how to obtain a _dbConnection for the desktop application.

var currentDir = Directory.GetCurrentDirectory();
var configBuilder = new ConfigurationBuilder();
configBuilder.SetBasePath(currentDir);
configBuilder.AddJsonFile("appsettings.json");
var config = configBuilder.Build();
_dbConnection = config.GetConnectionString("DefaultConnection");

Upvotes: 4

Related Questions