Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add an implementation of 'IDesignTimeDbContextFactory<DataContext>' to the project in asp.net-core-2.0

Here are the list of packages which I have installed : Installed Packages

I am using Entityframework core 2.0. First time I have successfully created database using entity framework code first migration(add-migration and update-database command). Now when I update my entities and try to run migration it's giving me following error.

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

Here is my code ...

Program.cs

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

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

Startup.cs

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)
{
    // Repositories

    services.AddMvc();
    services.AddDbContextPool<DataContext>(
        options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        //options => options.UseSqlServer(@"Server=LAPTOP-518D8067;Database=Gyanstack;Trusted_Connection=True;MultipleActiveResultSets=true"));

    services.AddCors();
    services.AddScoped<ISectionRepository, SectionRepository>();
    services.AddScoped(typeof(IEntityBaseRepository<>), typeof(EntityBaseRepository<>));
}

DataContext.cs

public class DataContext : DbContext
{
    public DataContext(DbContextOptions<DataContext> options) 
        : base(options)
    { }

    public DbSet<Section> Section { get; set; }
    public DbSet<SubSection> SubSection { get; set; }
    public DbSet<Article> Article { get; set; }
    public DbSet<Comment> Comment { get; set; }
    public DbSet<User> User { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.AddConfiguration(new SectionMap());
        modelBuilder.AddConfiguration(new SubSectionMap());
        modelBuilder.AddConfiguration(new ArticleMap());
        modelBuilder.AddConfiguration(new CommentMap());
    }
}
like image 346
M J Avatar asked Aug 26 '17 05:08

M J


2 Answers

EDIT: now obsolete. Microsoft updated their migration document at the end of September to show how to update so you do not need this workaround.

As seen on this issue I raised on github, you move your DB initialisation code to the Program main, putting it in between BuildWebHost() and .Run()

Its relatively easy once you understand you need to get the DB context using var context = services.GetRequiredService<MyContext>(); in Main and then everything works as expected. (though I still think DB initialisation is a one-time initialisation thing, not an every-program-run thing)

like image 157
gbjbaanb Avatar answered Jan 09 '23 11:01

gbjbaanb


Simply changed my Program.cs

from this

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

    host.Run();
  }
}

to this

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://wildermuth.com/2017/07/06/Program-cs-in-ASP-NET-Core-2-0

like image 36
Srinivas Mohan Pillalamarrri Avatar answered Jan 09 '23 11:01

Srinivas Mohan Pillalamarrri