RamenNoodle
RamenNoodle

Reputation: 107

EF Core - Unable to resolve service for type DbContext while attempting to activate ApplicationContext

I am attempting to scaffold CRUD razor pages using entity framework but the code generator throws the following error:

There was an error running the selected code generator: ‘Unable to resolve service for type ‘Microsoft.EntityFrameworkCore.DbContextOptions`1[TenthBatchTracker.Data.TenthContext’] while attempting to activate ‘TenthBatchTracker.Data.TenthContext’.’

Program.cs

using Microsoft.EntityFrameworkCore;
using TenthBatchTracker;
using TenthBatchTracker.Data;
using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

// DI for Automapper
builder.Services.AddAutoMapper(typeof(AutomapperProfile));

//Scaffolded Code to compare to mine
builder.Services.AddDbContext<TenthBatchTrackerContext1>(options =>

    options.UseSqlServer(builder.Configuration.GetConnectionString("TenthBatchTrackerContext1")));

// My DI for EF
builder.Services.AddDbContext<TenthContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("TenthContext")));




var app = builder.Build();


// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();

DbContext (DbSets Omitted)

using Microsoft.EntityFrameworkCore;
using TenthBatchTracker.Domain.Models;

namespace TenthBatchTracker.Data
{
    public class TenthContext : DbContext
    {

        public TenthContext (DbContextOptions<TenthContext> options) 
            : base(options)
        {

        }

     
    }
}

In my program.cs I am using builder.Services.AddDbContext for dependency injection of my DbContext class and the DbContext options. My class for my application DbContext is passing those options to the base constructor. I have also generated a new DBConext using the scaffolding tool and compared my code for my Db Context and DI injection with that of the generated code, and I don’t see a difference. My app builds and runs, I just can't scaffold pages. What am I missing?

Upvotes: 1

Views: 10423

Answers (1)

RamenNoodle
RamenNoodle

Reputation: 107

I ended up getting this to work by using the dotnet-aspnet-generator as Sedat had suggested. I also had to create a design-time factory for my context as described here - https://learn.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli.

dotnet-aspnet-codegenerator -p .\TenthBatchTracker\TenthBatchTracker.csproj razorpage Index List -m Material -dc TenthBatchTracker.Data.TenthContext -outDir Pages/Materials -udl -f

Upvotes: 0

Related Questions