Home > Back-end >  Unable to create an object of type 'AppContext'. Using verbose but
Unable to create an object of type 'AppContext'. Using verbose but

Time:05-19

I tried to do my first migration and I got this error.

I used:

dotnet ef migrations add initial --project AppProject.Core --verbose  

The error:

Finding application service provider in assembly 'AppProject.Core'...
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 'AppContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'AppContext'.

My Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

My Startup

services.AddDbContext<AppContext>(options =>
{
        options.UseNpgsql(connection);
});

services.AddIdentity<User, IdentityRole<Guid>>()
        .AddEntityFrameworkStores<SysFishingContext>()
        .AddDefaultTokenProviders();

I don't get what is the problem. Please someone help me.

CodePudding user response:

Try to specify both options --project and --startup-project

-p|--project <PROJECT>

This is the project that DbContext is sitting in.

-s|--startup-project <PROJECT>

This is the project with Depenedency Injection setup, usually the Web project.


Your migrations add command might look like below:

dotnet ef migrations add initial --startup-project AppProject.Web --project AppProject.Core --verbose  
  • Related