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

39,553

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.

Help Link

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
Share:
39,553

Related videos on Youtube

eman
Author by

eman

Updated on March 12, 2022

Comments

  • eman
    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
      Tseng over 4 years
      Please 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
      eman over 4 years
      @Tseng I Do That :)
    • Tseng
      Tseng over 4 years
      On 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
      eman over 4 years
      i change tag of my question to version 3.0 and 3.1 of .net core
  • eman
    eman over 4 years
    i use public ApplicationDbContext : IdentityDbContext<ApplicationUser> but the same error :(
  • Tseng
    Tseng over 4 years
    Show 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
    Tseng over 4 years
    Dont post code/extended information in comments, update your question...
  • Tseng
    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 called BuildWebhost. See migration docs. The naming and return type of that method is important for the dotnet 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
    AFetter almost 4 years
    apparently this should be marked as the correct answer.
  • Nicke Manarin
    Nicke Manarin over 3 years
    Yeah, thanks to verbose, I was able to identify that I forgot to add a parameterless constructor in my DbContext implementation and other issues.
  • John Pankowicz
    John Pankowicz over 3 years
    Yes, 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
    HackSlash over 3 years
    Please 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
    jao about 3 years
    This 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
    James Fleming almost 3 years
    you saved my bacon (or the vegan equivalent). Many thanks!
  • JokingBatman
    JokingBatman almost 3 years
    Thanks! This one helped me while upgrading to .netcore 3.1 from 2.2
  • JuztBe
    JuztBe almost 3 years
    Same here. I have added additional parameter to this method, that's why it broke.
  • Svinjica
    Svinjica over 2 years
    This one worked. Good job!
  • Admin
    Admin about 2 years
    As 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
    dpant about 2 years
    Thanks. If AppDbContext, the migrations and the model classes are defined in a Class Library separate from the project where appsettings.json and Startup.cs are located, you need to mark that project as the Startup Project (by right clicking on it) before executing Update-Database.
  • Dav.id
    Dav.id almost 2 years
    Excellent! 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!