1

When I want to create a controller I get this error:

error image

There was an error running the selected code generator:
'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Db.ContextOptions'1[DATAMain.DB] while attempting to activate 'DataMain.DB'

DB.cs


        using Microsoft.AspNetCore.Identity;
        using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
        using Microsoft.EntityFrameworkCore;
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        
        namespace DataMain
        {
            public class DB : IdentityDbContext<IdentityUser, IdentityRole, string>
            {
                public DB(DbContextOptions<DB> options) : base(options)
                {
                }

                public DbSet<Category> Categories { get; set; }
            }
         }
3

1 Answer 1

1

Be sure to have the Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore package installed.

then register the service :

using ContosoUniversity.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace ContosoUniversity
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
    -->     services.AddDbContext<SchoolContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

   -->      services.AddDatabaseDeveloperPageExceptionFilter();

            services.AddControllersWithViews();
        }

read documentation on microsoft : https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-6.0

Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't work due to a logical error in the framework version, it works fine with net 5.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.