【问题标题】:InvalidOperationException: Unable to resolve service for type '*Models.LandingPageContext' while attempting to activate Controller'InvalidOperationException:尝试激活控制器时无法解析类型“*Models.LandingPageContext”的服务
【发布时间】:2020-08-21 17:36:47
【问题描述】:

我有一个名为“LandingPage”的数据库,并使用此命令在我的 asp.net mvc 核心中映射了数据库表:-

 Scaffold-DbContext "Server=(localdb)\ProjectsV13;Database=LandingPage;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

然后我添加了一个控制器并选择了其中一个模型,但是当我尝试访问控制器时,我在网络浏览器中遇到了这个异常:-

An unhandled exception occurred while processing the request.
InvalidOperationException: Unable to resolve service for type 'LandingPage.Models.LandingPageContext' while attempting to activate 'LandingPage.Controllers.QuestionsController'.
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)
Stack Query Cookies Headers Routing 
InvalidOperationException: Unable to resolve service for type 'LandingPage.Models.LandingPageContext' while attempting to activate 'LandingPage.Controllers.QuestionsController'.
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)
lambda_method(Closure , IServiceProvider , object[] )
Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider+<>c__DisplayClass4_0.<CreateActivator>b__0(ControllerContext controllerContext)
Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider+<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

【问题讨论】:

    标签: asp.net-core asp.net-core-mvc asp.net-mvc-controller


    【解决方案1】:

    需要在 Startup.cs 中将 DbContext 注册为服务,以便在其构造函数中将其注入到控制器中:

    Startup.cs:

    services.AddDbContext<LandingPageContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    

    QuestionsController.cs:

    public class QuestionsController: Controller
    {
        public QuestionsController(LandingPageContext dbContext)
        {
        }
    }
    

    Dependency injection in ASP.NET Core 中的更多信息。

    【讨论】:

    • 我将代码添加到 startup.cs 但我在 services.AddDbContext&lt;LandingPageContext&gt;(options =&gt; options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 上得到空异常
    • 您的 appsettings.json 中是否有一个连接字符串,其键为 DefaultConnection?
    猜你喜欢
    • 2020-02-08
    • 2021-04-29
    • 2020-07-01
    • 2021-02-23
    • 2020-05-18
    • 2018-06-08
    • 2018-07-07
    • 2019-08-21
    • 1970-01-01
    相关资源
    最近更新 更多