Search code examples
asp.net-core-webapi.net-6.0visual-studio-2022entity-framework-6.4

There was an error running the selected code generator: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOption


I am using Visual Studio 2022 Preview and .NET 6 SDK.

Here I am creating a webAPI project with 2 layers. api project (Bgvsystem.webAPI) class library (BgvSystem.Persistance)

NuGet packages-

Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 6.0.0-rc.1.21452.10

Install-Package Microsoft.EntityFrameworkCore.Tools -Version 6.0.0-rc.1.21452.10

Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design -Version 6.0.0-rc.1.21464.1

When I try to add a controller using scaffolding, I get the below error

There was an error running the selected code generator: unable to resolve service for type 'microsoft.entityframeworkcore.dbcontextoption.. While attempting to activate Dbcontext in  .net 6 and visual studio 2022 preview

enter image description here

How to resolve this? Please help with this.


Solution

  • After struggling for 3 days, finally I found the mistake and fixed that.

    Actually I had to put connection string in MyApp.Persistance.DbContextClass as below

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                if (!optionsBuilder.IsConfigured)
                {
                    optionsBuilder.UseSqlServer("Data Source=DESKTOP-SV8GPJ2\\SQLEXPRESS;Initial Catalog=StarterAppDB;Persist Security Info=True;User ID=sa;Password=Admin@1234");
                }
            }
    

    Then it worked fine.