需要帮助注册依赖项

时间:2017-02-01 07:00:32

标签: c# asp.net-mvc entity-framework dependency-injection

我无法在Microsoft内置的依赖注入库中注册依赖项。我不知道从哪里去,我觉得到处都是。启动Web应用程序后,我收到以下错误:

  

InvalidOperationException:无法解析类型的服务   ' System.Data.Entity.DbContext'在尝试激活时   ' Services.Repositories.Repository`1 [Domain.Person]'

以下是我的Context类:

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());
    }

接下来是我的Repository类:

public class Repository<T> : IRepository<T> where T : class, IEntity
{
    protected DbSet<T> DbSet;

    public Repository(DbContext dataContext)
    {
        DbSet = dataContext.Set<T>();
    }

    public void Insert(T entity)
    {
        DbSet.Add(entity);
    }

    public void Delete(T entity)
    {
        DbSet.Remove(entity);
    }

    public IQueryable<T> GetAll()
    {
        return DbSet;
    }

    public T GetById(int id)
    {
        return DbSet.Find(id);
    }

    public IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate)
    {
        return DbSet.Where(predicate);
    }
}

我的服务类:

public class PersonService : IPersonService
{
    private readonly IRepository<Person> _repo;

    public PersonService(IRepository<Person> repo)
    {
        _repo = repo;
    }

    public IQueryable<Person> AsQueryable()
    {
        // TODO: Fix Queryable functionality
        return _repo.GetAll();
    }

    public Person CreatePerson(string firstName, string lastName, DateTime dob)
    {
        var person = new Person
        {
            FirstName = firstName,
            LastName = lastName,
            DOB = dob
        };
        return person;
    }

    public void DeletePerson(int id)
    {
        var person = _repo.GetById(id);
        _repo.Delete(person);
    }

    public Person UpdatePerson(int id, string firstname, string lastName, DateTime dob)
    {
        var person = _repo.GetById(id);
        person.FirstName = firstname;
        person.LastName = lastName;
        person.DOB = dob;
        return person;
    }
}

我的控制器类:

  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));
    }

我似乎缺少什么?这让我头疼了好几个小时。

1 个答案:

答案 0 :(得分:0)

您只是添加<div class="outer-section"> <div class="inner-section"> <div class="white-bg"> </div> <div class="button-section"> <div class="button-innner"> </div> </div> </div> </div>,但不将其与MyContext相关联。尝试像

这样的东西
DbContext

或将您的存储库更改为构造函数中的services.AddScoped<DbContext>( _ => new MyContext(Configuration.GetConnectionString("connString"))); 而不是MyContext