1

I'm using sqlserver2017 in docker in ubuntu os create asp.netcore 3 web app error in run dotnet ef add migrations firstmigrate

Unable to create an object of type 'DatabaseContext'. For the different patterns supported at design time,

image1

image2

Kartik Chauhan
  • 2,779
  • 5
  • 28
  • 39
  • Hello and welcome to SO! Please read the [tour](https://stackoverflow.com/tour), and [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) Please also read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Tomer Shetah Jan 16 '21 at 21:51

3 Answers3

1

Had similar problem after adding own DbContext constructor with (2) parameters. App was ok, but migrations stopped working.

Your looks similar - in case you do not need DatabaseContext constructor, try to remove it...

There is a build and DI running behind scenes.

Fix in my case:

  1. Update EntityFrameworkCore (3.1.5 used for strange reason, but solution had 5) using info from Dotnet tool @xspdf

    dotnet tool update --global dotnet-ef

  2. Removed my new DbContext constructor and replaced hardcoded connection string in OnConfiguring using Config class found in another answer here.

This command can show the most during add migration build/run in cmd.
Mind current dir is Migrations folder of (VS) startup project in my case.

dotnet ef --startup-project ../ --verbose migrations add test

3.1.5 & context activation Error

The Entity Framework tools version '3.1.5' is older than that of the runtime '5.0.0'. Update the tools for the latest features and bug fixes.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly '...'...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext '...Context'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type '...Context'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
 ---> System.InvalidOperationException: Unable to resolve service for type 'System.String' while attempting to activate '...'. (my additional parameter)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_4.<FindContextTypes>b__13()
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_4.<FindContextTypes>b__13()
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`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.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to create an object of type '...Context'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
Jan
  • 2,178
  • 3
  • 14
  • 26
0

Set your startup project to the one that you have registered "Application DbContext"

0

usually this error raises when you use multi context in your project. for solving this problem you should modify program.cs as follow: put follow code for each context

    builder.Services.AddDbContext<Context1>(options =>
    options.UseSqlServer(connectionString));


    builder.Services.AddDbContext<Context2>(options =>
    options.UseSqlServer(connectionString));