1

I'm trying to scaffold identity on UI SuplementShop.WEB.Mvc solution in Visual Studio 2022.

I'm new to ASP.NET so I hope somebody can help me :)

Open repository on GitHub: (version before scaffolding)

https://github.com/7LinesOfCode/SuplementShopWEB.MVC

Thanks in advance for every response :)

I want to make Login and Register scaffold, with data context "Context (Suplement.WEB.Mvc.Infrastructure)". After that step my app can't compile anymore.

Observations: after making scaffolding (as I said above) output in the console look like this:

enter image description here

After scaffolding if I try to debug my app, I get an exception in

var app = builder.Build()

enter image description here

List of errors:

 System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType:   

SuplementShopWEB.MVC.Application.Interfaces.IOrderService Lifetime: Transient ImplementationType:
SuplementShopWEB.MVC.Application.Services.OrderService': Unable to resolve service for type 'SuplementShopWEB.MVC.Infrastructure.Context' while attempting to activate 'SuplementShopWEB.MVC.Infrastructure.Repositories.OrderRepository'.) (Error while validating the service descriptor 'ServiceType: SuplementShopWEB.MVC.Application.Interfaces.ICustomerService Lifetime: Transient ImplementationType: SuplementShopWEB.MVC.Application.Services.CustomerService': Unable to resolve service for type 'SuplementShopWEB.MVC.Infrastructure.Context' while attempting to activate 'SuplementShopWEB.MVC.Infrastructure.Repositories.CustomerRepository'.) (Error while validating the service descriptor 'ServiceType: SuplementShopWEB.MVC.Application.Interfaces.IItemService Lifetime: Transient ImplementationType: SuplementShopWEB.MVC.Application.Services.ItemService': Unable to resolve service for type 'SuplementShopWEB.MVC.Infrastructure.Context' while attempting to activate 'SuplementShopWEB.MVC.Infrastructure.Repositories.ItemRepository'.) (Error while validating the service descriptor 'ServiceType: SuplementShopWEB.MVC.Domain.Interface.IOrderRepository Lifetime: Transient ImplementationType: SuplementShopWEB.MVC.Infrastructure.Repositories.OrderRepository': Unable to resolve service for type 'SuplementShopWEB.MVC.Infrastructure.Context' while attempting to activate 'SuplementShopWEB.MVC.Infrastructure.Repositories.OrderRepository'.) (Error while validating the service descriptor 'ServiceType: SuplementShopWEB.MVC.Domain.Interface.ICustomerRepository Lifetime: Transient ImplementationType: SuplementShopWEB.MVC.Infrastructure.Repositories.CustomerRepository': Unable to resolve service for type 'SuplementShopWEB.MVC.Infrastructure.Context' while attempting to activate 'SuplementShopWEB.MVC.Infrastructure.Repositories.CustomerRepository'.) (Error while validating the service descriptor 'ServiceType: SuplementShopWEB.MVC.Domain.Interface.IItemRepository Lifetime: Transient ImplementationType: SuplementShopWEB.MVC.Infrastructure.Repositories.ItemRepository': Unable to resolve service for type 'SuplementShopWEB.MVC.Infrastructure.Context' while attempting to activate 'SuplementShopWEB.MVC.Infrastructure.Repositories.ItemRepository'.)'

After that I can find a new connection string in appsettings.json.

Before scaffolding action, application had no errors and was working fine.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

After I cloned your project from github, I find the error and fix them, You can follow these steps.

First change your dbcontext class name, give it an unique name like MyApplicationDbcontext instead of just called Context, Then add related page in Scuffolding identity according to this dbcontext

enter image description here

Then click add button to add these identity pages.

Now you need to add-migration and update-database, First set your MVC project as the startup project, Then in Package Manager Console, set Infrastructure project as default project.

enter image description here

Finally use

add-migration MyMigration -Context MyApplicationDbcontext

update-database -Context MyApplicationDbcontext

to migration and create database.

enter image description here

Update:

enter image description here

Xinran Shen
  • 8,416
  • 2
  • 3
  • 12