Minimal Scaffold with EF does not work when `DbContext` declared in an external library
- Dominant language
- C#
- Stars
- 818
- Forks
- 260
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 10
Description
I have a very simple `DBContext`:
``` c#
public class SampleModel
{
public int Id { get; set; }
}
public class SampleModelContext : DbContext
{
public DbSet Samples => Set();
public SampleModelContext(DbContextOptions options)
: base(options)
{ }
}
```
That I have configured in my Minimal API application:
``` c#
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext(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.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.b__13()
at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.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)
```
Contributor guide
Assessment
This issue has not been assessed yet.