Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要帮助注册依赖项吗_C#_Asp.net Mvc_Entity Framework_Dependency Injection - Fatal编程技术网

C# 需要帮助注册依赖项吗

C# 需要帮助注册依赖项吗,c#,asp.net-mvc,entity-framework,dependency-injection,C#,Asp.net Mvc,Entity Framework,Dependency Injection,我在向Microsoft的内置依赖项注入库注册依赖项时遇到问题。我不知道从这里到哪里去,我感觉到处都是。启动web应用程序时,出现以下错误: InvalidOperationException:无法解析类型的服务 试图激活时出现“System.Data.Entity.DbContext” 'Services.Repositories.Repository'1[Domain.Person]' 下面是我的上下文类: public class MyContext : DbContext { p

我在向Microsoft的内置依赖项注入库注册依赖项时遇到问题。我不知道从这里到哪里去,我感觉到处都是。启动web应用程序时,出现以下错误:

InvalidOperationException:无法解析类型的服务 试图激活时出现“System.Data.Entity.DbContext” 'Services.Repositories.Repository'1[Domain.Person]'

下面是我的上下文类:

public class MyContext : DbContext
{
    public MyContext(string connString) : base(connString) { }

    public DbSet<Person> People { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        modelBuilder.Configurations.Add(new PersonMap());
    }
到目前为止,我在这里登记了所有信息:

public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();
        services.AddScoped<MyContext>(_ => new MyContext(Configuration.GetConnectionString("connString")));
        services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
        services.AddScoped(typeof(IPersonService), typeof(PersonService));
    }
public void配置服务(IServiceCollection服务)
{
//添加框架服务。
services.AddMvc();
AddScoped(=>newmyContext(Configuration.GetConnectionString(“connString”));
addScope(typeof(IRepository)、typeof(Repository));
AddScoped(typeof(IPersonService)、typeof(PersonService));
}

我似乎遗漏了什么?这让我头痛了好几个小时。

您只添加了
MyContext
,但没有将其与
DbContext
关联。试试像这样的东西

services.AddScoped<DbContext>( _ => new MyContext(Configuration.GetConnectionString("connString")));
services.AddScoped(=>newmyContext(Configuration.GetConnectionString(“connString”));

或者将存储库更改为在构造函数中使用
MyContext
,而不是
DbContext

!这么简单的修复。非常感谢你,你的回答解决了我的问题。
  public class HomeController : Controller
{
    private readonly IPersonService _personService;
    public HomeController(IPersonService personService)
    {
        _personService = personService;
    }

    public IActionResult Index()
    {
        return View();
    }
}
public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();
        services.AddScoped<MyContext>(_ => new MyContext(Configuration.GetConnectionString("connString")));
        services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
        services.AddScoped(typeof(IPersonService), typeof(PersonService));
    }
services.AddScoped<DbContext>( _ => new MyContext(Configuration.GetConnectionString("connString")));