Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time
Solution 1
Seems you are your inheritance is wrong.
public ApplicationDbContext : IdentityDbContext
should be
public ApplicationDbContext : IdentityDbContext<ApplicationUser>
or
public ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole>
if you also extend roles class.
when you want to create an context with an extended user class (instead of IdentityUser
)
Solution 2
I found the cause of this error could be multiple things in your code. For me at least, the best way was to add verbose in command.
With that will be able to understand what is the problem. the verbose
will display all steps of the execution.
In visual studio use:
add-migration Added_something -verbose
For the CLI use:
dotnet ef migrations add Added_something --verbose
Solution 3
This error can also occur if multiple startup projects is selected. I set my webproject to startup project and that solved the issue for me.
Solution 4
My problem was solved by installing Microsoft.EntityFrameworkCore.Design
nuget package.
this package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct.then install the package.
at the end Build -> Clean Solution in your project and then try running your command again.
add migration command cli:
dotnet ef migrations add InitDatabase --project YourDataAccessLibraryName -s YourWebProjectName -c YourDbContextClassName --verbose
update database command cli:
dotnet ef database update InitDatabase --project YourDataAccessLibraryName -s YourWebProjectName -c YourDbContextClassName --verbose
remove migration command cli:
dotnet ef migrations remove --project YourDataAccessLibraryName -s YourWebProjectName -c YourDbContextClassName --verbose
Entity Framework Core tools reference - .NET Core CLI
Solution 5
I also had same problem today when I was running the dotnet ef migrations add <MigrationName>
I had three project, MainApp (Web), C# Project with DBContext and C# Project for Models.
I was able to resolve it from CLI.
dotnet ef migrations add AddCategoryTableToDb -s MainApp -p ProjectHavingDbContext
Related videos on Youtube
eman
Updated on March 12, 2022Comments
-
eman about 2 years
I face the following error when adding the migration of database in .net core
This is the error:
This is the code in
Startup
:public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddDefaultIdentity<ApplicationUser>().AddEntityFrameworkStores<ApplicationDbContext>(); services.AddControllers(); }
This is the
ApplicationDbContext
class:public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public DbSet<ApplicationUser> applicationUsers { get; set; } }
This is the
ApplicationUser
:public class ApplicationUser : IdentityUser { [Required] [Column(TypeName = "nvarchar(150)")] public string UserFName { get; set; } [Required] public string UserLName { get; set; } }
-
Tseng over 4 yearsPlease don't post images, post code (see editing help). On a side-note, please check the C# Coding Guidelines using non-standard naming conventions makes the code much harder to read for any C# developer ;)
-
eman over 4 years@Tseng I Do That :)
-
Tseng over 4 yearsOn top of the comments below my answer, your question is rather misldeading. What version are you using? Question is tagged asp.net core 2.0 and 2.1, but your error message mentions 3.1 ??? So: a) which Version of ASP.NET Core are you using? b) which verison of the SDKs you have installed?
-
eman over 4 yearsi change tag of my question to version 3.0 and 3.1 of .net core
-
-
eman over 4 yearsi use public
ApplicationDbContext : IdentityDbContext<ApplicationUser>
but the same error :( -
Tseng over 4 yearsShow your Program.cs file, not sure if you are using some outdated pattern (i.e. dotnet tooling method names from 2.1 but having a 3.0 or 3.1 application
-
Tseng over 4 yearsDont post code/extended information in comments, update your question...
-
Tseng over 4 years
CreateHostBuilder
is for ASP.NET Core 2.1 and newer. If you are still targeting ASP.NET Core 2.0, it won't be recognized and should be calledBuildWebhost
. See migration docs. The naming and return type of that method is important for thedotnet
tooling to work (such as ef core command lines and migrations), since it look look in this method to know how to instantiate a DbContext for the migrations -
AFetter almost 4 yearsapparently this should be marked as the correct answer.
-
Nicke Manarin over 3 yearsYeah, thanks to
verbose
, I was able to identify that I forgot to add a parameterless constructor in myDbContext
implementation and other issues. -
John Pankowicz over 3 yearsYes, this is the much better answer. My solution was totally different than others, but -verbose found it easily. "Give a man a fish and you feed him for a day; teach a man to fish, you feed him for a lifetime."
-
HackSlash over 3 yearsPlease just explain a clear step by step of how someone else would replicate your solution without apology. Sometimes a new answer can be useful to someone later on. Thanks for helping.
-
jao about 3 yearsThis was also the case for me. I think it couldn't load the settings from the appsettings.json, because that was handled by the startup.cs.
-
James Fleming almost 3 yearsyou saved my bacon (or the vegan equivalent). Many thanks!
-
JokingBatman almost 3 yearsThanks! This one helped me while upgrading to .netcore 3.1 from 2.2
-
JuztBe almost 3 yearsSame here. I have added additional parameter to this method, that's why it broke.
-
Svinjica over 2 yearsThis one worked. Good job!
-
Admin about 2 yearsAs it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-
dpant about 2 yearsThanks. If
AppDbContext
, the migrations and the model classes are defined in a Class Library separate from the project whereappsettings.json
andStartup.cs
are located, you need to mark that project as the Startup Project (by right clicking on it) before executingUpdate-Database
. -
Dav.id almost 2 yearsExcellent! such an easy miss! looked all over but I guess during the transition from startup.cs to the new minimal hosting (program.cs) people like myself are missing little things like this! thank you! fixed the issue in the new dotnet 6 way!