2

I have a Persistence project which includes my ApplicationDbContext class and it is this project where I want my migrations to be created.

Persistence.Context.DbContext

public class PizzaDeliveryDbContext : DbContext
{
    private readonly IConfiguration _config;

    public PizzaDeliveryDbContext(IConfiguration config)
    {
        _config = config;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql(_config.GetConnectionString("pizza"));
    }

    // each dbset maps to a table that will be created in the database
    public DbSet<Customer> Customers { get; set; }

    public DbSet<Order> Orders { get; set; }

    public DbSet<Product> Products { get; set; }

    public DbSet<OrderDetail> OrderDetails { get; set; }

}

In my startup project (separate from Persistence)

using Microsoft.EntityFrameworkCore;
using Persistence.Context;

var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("pizza");

// register dbcontext
builder.Services.AddDbContext<PizzaDeliveryDbContext>(options =>
    options.UseNpgsql(connectionString));

var app = builder.Build();

When I try to run dotnet ef migrations add InitialCreate (pointed to persistence directory) - I get the following error:

/user Persistence % dotnet ef migrations add InitialCreate
Build started...
Build succeeded.
Unable to create an object of type 'PizzaDeliveryDbContext'. For 

the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Running dotnet ef migrations add InitialCreate -verbose

Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'Persistence'...
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 'PizzaDeliveryDbContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'PizzaDeliveryDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
 ---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[Persistence.Context.PizzaDeliveryDbContext]' while attempting to activate 'Persistence.Context.PizzaDeliveryDbContext'.
   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__DisplayClass21_4.<FindContextTypes>b__13()
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass21_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 'PizzaDeliveryDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Persistence project I have installed: Microsoft.EntityFrameworkCore 7.0.5, Microsoft.EntityFrameworkCore.Design 7.0.5, Microsoft.EntityFrameworkCore.Tools 7.0.5, Npgsql.EntityFrameworkCore.PostreSQL 7.0.3

J Moss
  • 205
  • 1
  • 5
  • 13

1 Answers1

0

Found the answer using this question. I am using visual studio for mac so I found out that when running Add-migration, it seems to think my migration project and startup project are the same.

Of course this is not the case, so I had specify the project path like so.

dotnet ef migrations add InitialCreate --project ../Persistence --startup-project ../pizza-delivery

J Moss
  • 205
  • 1
  • 5
  • 13