dotnet / Scaffolding

Code generators to speed up development.
MIT License
640 stars 228 forks source link

Minimal Scaffold with EF does not work when `DbContext` declared in an external library #1962

Open brunolins16 opened 2 years ago

brunolins16 commented 2 years ago

I have a very simple DBContext:

public class SampleModel
{
    public int Id { get; set; }
}

public class SampleModelContext : DbContext
{
    public DbSet<SampleModel> Samples => Set<SampleModel>();

    public SampleModelContext(DbContextOptions<SampleModelContext> options)
        : base(options)
    {   }
}

That I have configured in my Minimal API application:

using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<SampleModelContext>(options
    => _ = options.UseSqlServer(builder.Configuration.GetConnectionString("SampleDB") ??
    throw new InvalidOperationException("Connection string 'SampleDB' not found.")));

var app = builder.Build();

app.Run();

I can scaffold without problems If the SampleModel is part of my Minimal API assembly, however, when moving it to a new assembly I start getting:

Unable to create an object of type 'SampleModelContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 StackTrace:
Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[ClassLibrary1.SampleModelContext]' while attempting to activate 'ClassLibrary1.SampleModelContext'.   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.DbContextActivator.CreateInstance(Type contextType, Assembly startupAssembly, IOperationReportHandler reportHandler, String[] args)
   at Microsoft.EntityFrameworkCore.Design.DbContextActivator.CreateInstance(Type contextType, Assembly startupAssembly, IOperationReportHandler reportHandler)
   at Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.EntityFrameworkModelProcessor.TryCreateContextUsingAppCode(Type dbContextType, Type startupType)
Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[ClassLibrary1.SampleModelContext]' while attempting to activate 'ClassLibrary1.SampleModelContext'. StackTrace:
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass21_4.<FindContextTypes>b__13()
   at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine>b__6_0()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args)
   at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
FaouziSelmi commented 2 years ago

The similar problem is discussed here: https://github.com/dotnet/Scaffolding/issues/1765