title: ASP.NET Core 2 Unable to resolve service for type Microsoft EntityFrameworkCore DbContext
body: <p>When I run my asp.net core 2 projects I get the following error message:</p>
  
  <blockquote>
    <p>InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContext' while attempting to activate 'ContosoUniversity.Service.Class.StudentService'.</p>
  </blockquote>
  
  <p>Here is my project structure:</p>
  
  <pre><code>-- solution 'ContosoUniversity'
  ----- ContosoUniversity
  ----- ContosoUniversity.Model
  ----- ContosoUniversity.Service
  </code></pre>
  
  <p><strong>IEntityService (related code) :</strong></p>
  
  <pre><code>public interface IEntityService&lt;T&gt; : IService
   where T : BaseEntity
  {
      Task&lt;List&lt;T&gt;&gt; GetAllAsync();      
  }
  </code></pre>
  
  <p><strong>IEntityService (related code) :</strong></p>
  
  <pre><code>public abstract class EntityService&lt;T&gt; : IEntityService&lt;T&gt; where T : BaseEntity
  {
      protected DbContext _context;
      protected DbSet&lt;T&gt; _dbset;
  
      public EntityService(DbContext context)
      {
          _context = context;
          _dbset = _context.Set&lt;T&gt;();
      }
  
      public async virtual Task&lt;List&lt;T&gt;&gt; GetAllAsync()
      {
          return await _dbset.ToListAsync&lt;T&gt;();
      }
  }
  </code></pre>
  
  <p><strong>Entity :</strong></p>
  
  <pre><code>public abstract class BaseEntity { 
  
  }
  
  public abstract class Entity&lt;T&gt; : BaseEntity, IEntity&lt;T&gt; 
  {
      public virtual T Id { get; set; }
  }
  </code></pre>
  
  <p><strong>IStudentService :</strong></p>
  
  <pre><code>public interface IStudentService : IEntityService&lt;Student&gt;
  {
      Task&lt;Student&gt; GetById(int Id);
  }
  </code></pre>
  
  <p><strong>StudentService :</strong></p>
  
  <pre><code>public class StudentService : EntityService&lt;Student&gt;, IStudentService
  {
      DbContext _context;
  
      public StudentService(DbContext context)
          : base(context)
      {
          _context = context;
          _dbset = _context.Set&lt;Student&gt;();
      }
  
      public async Task&lt;Student&gt; GetById(int Id)
      {
          return await _dbset.FirstOrDefaultAsync(x =&gt; x.Id == Id);
      }
  }
  </code></pre>
  
  <p><strong>SchoolContext :</strong></p>
  
  <pre><code>public class SchoolContext : DbContext
  {
      public SchoolContext(DbContextOptions&lt;SchoolContext&gt; options) : base(options)
      {
      }
  
      public DbSet&lt;Course&gt; Courses { get; set; }
      public DbSet&lt;Enrollment&gt; Enrollments { get; set; }
      public DbSet&lt;Student&gt; Students { get; set; }
  }
  </code></pre>
  
  <p>And finally here is my <strong>Startup.cs</strong> class : </p>
  
  <pre><code>public class Startup
  {
      public Startup(IConfiguration configuration, IHostingEnvironment env, IServiceProvider serviceProvider)
      {
          Configuration = configuration;
  
          var builder = new ConfigurationBuilder()
          .SetBasePath(env.ContentRootPath)
          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
          .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
  
  
          Configuration = builder.Build();
  
      }
  
      public IConfiguration Configuration { get; }
  
      // This method gets called by the runtime. Use this method to add services to the container.
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddDbContext&lt;SchoolContext&gt;(option =&gt;
              option.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
  
  
          services.AddScoped&lt;IStudentService, StudentService&gt;();
  
          services.AddMvc();
      }
  
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
              app.UseBrowserLink();
          }
          else
          {
              app.UseExceptionHandler("/Home/Error");
          }
  
          app.UseStaticFiles();
  
          app.UseMvc(routes =&gt;
          {
              routes.MapRoute(
                  name: "default",
                  template: "{controller=Home}/{action=Index}/{id?}");
          });
      }
  }
  </code></pre>
  
  <p>What should I do to resolve this problem?</p>