5

I had a .NET core 1.0 webapp working fine. I had to upgrade to .NET Core 2.0. I also had to add a migration step for my SQLite database.

If I launch this command:

Add-Migration MyMigrationStepName

I get this error:

Unable to create an object of type 'ServicesDbContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.

I've seen plenty of answers on SO and on other blogs and website but none of them actually say where to implement such interface and what code the concrete method should contain!

Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159

3 Answers3

6

Sample:

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
}

public class ApplicationContextDbFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
    ApplicationDbContext IDesignTimeDbContextFactory<ApplicationDbContext>.CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
        optionsBuilder.UseSqlServer<ApplicationDbContext>("Server = (localdb)\\mssqllocaldb; Database = MyDatabaseName; Trusted_Connection = True; MultipleActiveResultSets = true");

        return new ApplicationDbContext(optionsBuilder.Options);
    }
}

Location:

Put that class where the ApplicationDbContext is put. It will then be automatically picked up when you use dotnet ef cli commands.

Sergei Danielian
  • 4,938
  • 4
  • 36
  • 58
Pascal
  • 12,265
  • 25
  • 103
  • 195
1

A possible solution to IDesignTimeDbContextFactory<> problem is the DBContext discovery/launch during the add-migration process. Add migrations needs to get a context. If it cant, this error is thrown. This can happen if you do not have a public parameter-less DBContext. So a solution is to Add a public Parameter-less constructor to your context.

public  class SomeDbContext: DbContext
{
    // this PUBLIC constructor  is required for Migration tool
    public SomeDbContext()
    {

    }
  // the model...
    public DbSet<PocoBla> PocoBlas { get; set; }
    ....
 }

You can have a special version of your DBContext in a separate project .netCore console project for migration code generation purposes.

phil soady
  • 11,043
  • 5
  • 50
  • 95
  • 1
    this will probably cause 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` – Daniël Tulp Dec 03 '18 at 10:14
  • @DaniëlTulp yes, you are correct. It definitely throws an error. The DbContext will be constantly in search of a constructor in this case and then you have to work as Pascal wrote in his answer. – Rohan Rao Dec 24 '19 at 11:15
1

I solve the problem by simply updating Program.cs to the latest .NET Core 2.x pattern:

From 1.x:

using System.IO; using Microsoft.AspNetCore.Hosting;

namespace AspNetCoreDotNetCore1App {
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .UseApplicationInsights()
                .Build();

            host.Run();
        }
    } }

To 2.x:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace AspNetCoreDotNetCore2App
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

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

Source: https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation

mejiamanuel57
  • 6,807
  • 1
  • 32
  • 34