title: Unable to create an object of type 'ApplicationDbContext'. Ef core 5.0
body: <p>I using CleanArchitecture solution. I have Data layer where ApplicationDbContext and UnitOfWork are located :</p>
  <pre><code>namespace Portal.Data.MyDbContexts
  {
      internal class ApplicationDbContext : DbContext
      {
  
          public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options) : base(options)
          {
              Database.EnsureCreated();
          }
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              base.OnModelCreating(modelBuilder);
              modelBuilder.ApplyConfigurationsFromAssembly(typeof(BaseEntity).Assembly);
          }
  
  
          // **********
          public DbSet&lt;WebsiteMonitoredCategory&gt; WebsiteMonitoredCategories { get; set; }
          // **********
  
  
          
      }
  </code></pre>
  <p>unitofwork:</p>
  <pre><code>public abstract class BaseUnitOfWork : object, IBaseUnitOfWork
      {
          //public UnitOfWork() : base()
          //{
          //}
  
          public BaseUnitOfWork(Options options) : base()
          {
              Options = options;
          }
  
          // **********
          protected Options Options { get; set; }
          // **********
  
          // **********
          // **********
          // **********
          private ApplicationDbContext _databaseContext;
          // **********
  
          // **********
          /// &lt;summary&gt;
          /// Lazy Loading = Lazy Initialization
          /// &lt;/summary&gt;
          internal ApplicationDbContext DatabaseContext
          {
              get
              {
                  if (_databaseContext == null)
                  {
                      var optionsBuilder =
                          new DbContextOptionsBuilder&lt;ApplicationDbContext&gt;();
  
                      switch (Options.Provider)
                      {
                          case Provider.SqlServer:
                              {
                                  optionsBuilder.UseSqlServer
                                      (connectionString: Options.ConnectionString);
  
                                  break;
                              }
  
                          case Provider.MySql:
                              {
                                  //optionsBuilder.UseMySql
                                  //  (connectionString: Options.ConnectionString);
  
                                  break;
                              }
  
                          case Provider.Oracle:
                              {
                                  //optionsBuilder.UseOracle
                                  //  (connectionString: Options.ConnectionString);
  
                                  break;
                              }
  
                          case Provider.PostgreSQL:
                              {
                                  //optionsBuilder.UsePostgreSQL
                                  //  (connectionString: Options.ConnectionString);
  
                                  break;
                              }
  
                          case Provider.InMemory:
                              {
                                  optionsBuilder.UseInMemoryDatabase(databaseName: &quot;Temp&quot;);
  
                                  break;
                              }
  
                          default:
                              {
                                  break;
                              }
                      }
  
                      _databaseContext =
                          new ApplicationDbContext(options: optionsBuilder.Options);
                  }
  
                  return _databaseContext;
              }
          }
  </code></pre>
  <p>and I have IoC Layer too for dependency injection to presentation layer:</p>
  <pre><code>public class DependencyContainer
      {
          public static void RegisterServices(IServiceCollection services,IConfiguration configuration)
          {
  
              
              //DataLayer
              services.AddTransient&lt;IUnitOfWork, UnitOfWork&gt;(_ =&gt;
              {
                  Options options =
                      new Options
                      {
                          Provider =
                              (Provider)System.Convert.ToInt32(configuration.GetSection(key: &quot;DatabaseProvider&quot;).Value),
  
                      
  
                          ConnectionString =
                              configuration.GetSection(key: &quot;ConnectionStrings&quot;).GetSection(key: &quot;MyConnectionString&quot;).Value,
                      };
  
                  return new UnitOfWork(options: options);
              });
  
  
          }
      }
  </code></pre>
  <p>and finally I Have an Windows Forms application Net 5.0 (All Projects are Net5.0)</p>
  <pre><code>namespace Portal.Desktop
  {
      public static class Program
      {
          private static IConfiguration Configuration { get; set; }
          public static IServiceProvider ServiceProvider { get; set; }
          /// &lt;summary&gt;
          ///  The main entry point for the application.
          /// &lt;/summary&gt;
          [STAThread]
          static void Main()
          {
              var services = ConfigureServices();
  
              var builder = new ConfigurationBuilder()
                  .SetBasePath(Directory.GetCurrentDirectory())
                  .AddJsonFile(&quot;appsettings.json&quot;, optional: true, reloadOnChange: true);
              Configuration = builder.Build();
  
              
          
  
              Application.SetHighDpiMode(HighDpiMode.SystemAware);
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
              Application.Run(new MainForm());
              RegisterServices(services, Configuration);
          }
  
          public static void RegisterServices(IServiceCollection services, IConfiguration configuration)
          {
              DependencyContainer.RegisterServices(services, configuration);
          }
  
          public static IServiceCollection ConfigureServices()
          {
              var services = new ServiceCollection();
              //services.AddTransient&lt;IUnitOfWork, UnitOfWork&gt;(_ =&gt;
              //{
              //  Options options =
              //      new Options
              //      {
              //          Provider =
              //              (Provider)System.Convert.ToInt32(Configuration.GetSection(key: &quot;DatabaseProvider&quot;).Value),
  
              //          //using Microsoft.EntityFrameworkCore;
              //          //ConnectionString =
              //          //  Configuration.GetConnectionString().GetSection(key: &quot;MyConnectionString&quot;).Value,
  
              //          ConnectionString =
              //              Configuration.GetSection(key: &quot;ConnectionStrings&quot;).GetSection(key: &quot;MyConnectionString&quot;).Value,
              //      };
  
              //  return new Portal.Data.UoW.UnitOfWork(options: options);
              //});
  
  
              ServiceProvider = services.BuildServiceProvider();
  
              return services;
          }
      }
  }
  </code></pre>
  <p>I usage Ef core 5.0 , windows forms is set as startup project and package  manager console  set to Protal.Data (dbcontext and unitofwork in there)
  but when i run <em>Add-Migration inti</em> in PMC get me an error:</p>
  <blockquote>
  <p>Unable to create an object of type 'ApplicationDbContext'. For the
  different patterns supported at design time, see
  <a href="https://go.microsoft.com/fwlink/?linkid=851728" rel="nofollow noreferrer">https://go.microsoft.com/fwlink/?linkid=851728</a></p>
  </blockquote>
  <p>installed  this packages in data layer:</p>
  <pre><code>&lt;ItemGroup&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore&quot; Version=&quot;5.0.7&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.Design&quot; Version=&quot;5.0.7&quot;&gt;
          &lt;IncludeAssets&gt;runtime; build; native; contentfiles; analyzers; buildtransitive&lt;/IncludeAssets&gt;
      &lt;/PackageReference&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.InMemory&quot; Version=&quot;5.0.7&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.SqlServer&quot; Version=&quot;5.0.7&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.Tools&quot; Version=&quot;5.0.7&quot;&gt;
          &lt;IncludeAssets&gt;runtime; build; native; contentfiles; analyzers; buildtransitive&lt;/IncludeAssets&gt;
      &lt;/PackageReference&gt;
    &lt;/ItemGroup&gt;
  </code></pre>
  <p>I don't know why, get me the error.</p>
  <p>-----UPDATE----</p>
  <p>I follow the <a href="https://go.microsoft.com/fwlink/?linkid=851728" rel="nofollow noreferrer">https://go.microsoft.com/fwlink/?linkid=851728</a> and added <em>ApplicationDbContextFactory.cs</em> to Portal.Desktop (windows forms Net5.0) project:</p>
  <pre><code>namespace Portal.Desktop
  {
      public class ApplicationDbContextFactory:IDesignTimeDbContextFactory&lt;ApplicationDbContext&gt;
      {
          public ApplicationDbContext CreateDbContext(string[] args)
          {
              var connectionString = Program.Configuration.GetSection(key: &quot;ConnectionStrings&quot;).GetSection(key: &quot;MyConnectionString&quot;).Value;
              var provider = Program.Configuration.GetSection(key: &quot;DatabaseProvider&quot;).Value;
              var optionsBuilder =
                  new DbContextOptionsBuilder&lt;ApplicationDbContext&gt;();
              var options =
                  new Options
                  {
                      Provider =
                          (Provider)System.Convert.ToInt32(provider),
  
                      //using Microsoft.EntityFrameworkCore;
                      //ConnectionString =
                      //  Configuration.GetConnectionString().GetSection(key: &quot;MyConnectionString&quot;).Value,
  
                      ConnectionString = connectionString,
                  };
              switch (options.Provider)
              {
                  case Provider.SqlServer:
                  {
                      optionsBuilder.UseSqlServer
                          (connectionString: options.ConnectionString);
  
                      break;
                  }
  
                  case Provider.MySql:
                  {
                      //optionsBuilder.UseMySql
                      //  (connectionString: Options.ConnectionString);
  
                      break;
                  }
  
                  case Provider.Oracle:
                  {
                      //optionsBuilder.UseOracle
                      //  (connectionString: Options.ConnectionString);
  
                      break;
                  }
  
                  case Provider.PostgreSQL:
                  {
                      //optionsBuilder.UsePostgreSQL
                      //  (connectionString: Options.ConnectionString);
  
                      break;
                  }
  
                  case Provider.InMemory:
                  {
                      optionsBuilder.UseInMemoryDatabase(databaseName: &quot;Temp&quot;);
  
                      break;
                  }
  
                  default:
                  {
                      break;
                  }
              }
  
              return 
                  new ApplicationDbContext(options: optionsBuilder.Options);
          }
      }
  }
  </code></pre>
  <p>When I want add a migration ef core throw <em>Exception has been thrown by the target of an invocation.</em> exception:</p>
  <blockquote>
  <p>System.Reflection.TargetInvocationException: Exception has been thrown
  by the target of an invocation.  ---&gt; System.NullReferenceException:
  Object reference not set to an instance of an object.    at
  Portal.Desktop.ApplicationDbContextFactory.CreateDbContext(String[]
  args) in C:\Users\Arman
  Es\source\repos\Spider\Portal.Desktop\ApplicationDbContextFactory.cs:line
  20    --- End of inner exception stack trace ---    at
  System.RuntimeMethodHandle.InvokeMethod(Object target, Object[]
  arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
  at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
  invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)<br />
  at System.Reflection.MethodBase.Invoke(Object obj, Object[]
  parameters)    at
  Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContextFromFactory(Type
  factory, Type contextType)    at
  Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.&lt;&gt;c__DisplayClass13_2.b__9()
  at
  Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func<code>1 factory)    at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)    at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.&lt;&gt;c__DisplayClass0_0.&lt;.ctor&gt;b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.&lt;&gt;c__DisplayClass3_0</code>1.b__0()
  at
  Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action
  action) Exception has been thrown by the target of an invocation.</p>
  </blockquote>