【问题标题】:Unable to resolve service for type [DBcontext] while attempting to activate page model尝试激活页面模型时无法解析类型 [DBcontext] 的服务
【发布时间】:2019-10-24 02:21:03
【问题描述】:

我目前正在为我的 A-Level Computing 编写一个 Asp.Net 项目。

我正在尝试访问我的数据库上下文 (UserGameKeyContext),以便我可以通过添加新记录来更新它。但是,当我初始化页面 /Play 时(我试图在其中添加这个新记录)我得到这个错误;

InvalidOperationException: Unable to resolve service for type 
'Project_With_Identity.Models.UserGameKeyContext' while attempting to activate 
'Project_With_Identity.Pages.PlayModel'. 

我目前的问题是,我看到的与此错误相关的所有其他答案都与接口相关,而不是与数据库相关。

这里是/播放

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Razor;
using Project_With_Identity.Hubs;
using Microsoft.AspNetCore.Identity;
using Project_With_Identity.Areas.Identity.Data;
using Project_With_Identity.Models;

namespace Project_With_Identity.Pages
{
    public class PlayModel : PageModel
    {
        private readonly UserManager<Project_With_IdentityUser> _userManager;
        private readonly SignInManager<Project_With_IdentityUser> _signInManager;
        private readonly UserGameKeyContext _gamekeycontext;
        public PlayModel(
            UserManager<Project_With_IdentityUser> userManager,
            SignInManager<Project_With_IdentityUser> signInManager,
            UserGameKeyContext gameKeyContext)
        {
            _userManager = userManager;
            _signInManager = signInManager;
            _gamekeycontext = gameKeyContext; 
        }

        //edited for Brevity
    }
} 

UserGameKeyContext gameKeyContext 似乎是问题所在 - 每当我尝试加载 /Play 页面时,都会收到上述错误。

这里也是 UserGameKeyContext;

using System.Linq;
using System.Threading.Tasks;
using Project_With_Identity.Models;
using Project_With_Identity.Areas.Identity;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace Project_With_Identity.Models
{
    public class UserGameKeyContext : DbContext
    {
        public UserGameKeyContext() : base("UserGameKeyContext")
            {
            }

        public DbSet<UserGameKey> UserGameKeys { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Add();
        }
    }
}

然后,我要做的是让 /play 加载 UserGameKeyContext,以便稍后在我的 /play 代码中我有一个可以访问和修改的上下文变量,主要是这样我可以添加新记录到数据库。如果我完全错误地初始化了这个数据库,我很想知道这样我才能回到正确的轨道上——否则,我们将不胜感激帮助理解和解决这个问题。谢谢!

【问题讨论】:

    标签: c# asp.net razor-pages


    【解决方案1】:

    您必须使用 .NET 依赖注入注册所有注入类型,无论它们是接口还是具体类。

    Startup.cs中,您可以执行以下操作:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        // Other stuff...
        services.AddDbContext<UserGameKeyContext>();
    }
    

    进一步阅读:

    https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

    【讨论】:

    • 我添加了Services.AddDbContext,和标准模板Services.AddDbContext基本一模一样,但是现在出现异常; ``` 无法将 lambda 表达式转换为类型 'ServiceLifetime' 因为它不是委托类型 ``` 错误。我不确定出了什么问题,试图在谷歌上找到答案并没有得到任何有用的修复
    • @IsabelleSHACKLETON 你能用 ConfigureServices 的代码更新你的问题吗?
    • 不需要,我已经设法解决了这个问题 - 我的 Context 类正在使用 System.Data.Entity 并且是这样构建的,而不是像 EntityFrameworkCore 上下文那样构建的。我修改了上下文,使其像在 EntityFrameworkCore 中一样构建,并且因此我设法使数据库的迁移工作。感谢您的帮助
    • @IsabelleSHACKLETON 很高兴听到您成功了。从 .NET 到 .NET Core 的过渡有点混乱,因为各种细微的变化和文档没有明确说明,所以我很同情。如果我提供的答案有助于解决原始依赖注入问题,请将答案标记为已接受。谢谢
    猜你喜欢
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 2018-09-24
    相关资源
    最近更新 更多