title: .NET 6 ... unable to resolve service for type
body: <p>I'm using NuGet packages in VS 2022:</p>
  <p>Microsoft.EntityFrameworkCore.SQLServer
  Microsoft.EntityFrameworkCore.Tools</p>
  <p>Connected to my SQL server (all good), all my models partial classes generated.  I add a new controller (Add New Scaffolded Item) and select &quot;MVC Controller with views, using Entity Framework&quot;.  Select my Model class &quot;Phonenumbertype&quot; my Data context class &quot;JMSContext&quot; (just web API no front end tested thru Swagger).  My controller gets generated</p>
  <pre><code>namespace WebAppTest.Controllers
  {
      [Route(&quot;api/[controller]&quot;)]
      [ApiController]
      public class PhonenumbertypesController : ControllerBase
      {
  
      // Error out with code generated:
      // https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-6.0
      // https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-6.0
  
      // Possible Solution
      // https://stackoverflow.com/questions/40900414/asp-net-core-dependency-injection-error-unable-to-resolve-service-for-type-whil
  
      private readonly JMSContext _context;
  
      public PhonenumbertypesController(JMSContext context)
      {
          _context = context;
      }
  
      // GET: api/Phonenumbertypes
      [HttpGet]
      public async Task&lt;ActionResult&lt;IEnumerable&lt;Phonenumbertype&gt;&gt;&gt; GetPhonenumbertypes()
      {
          return await _context.Phonenumbertypes.ToListAsync();
      }
  
      // GET: api/Phonenumbertypes/5
      [HttpGet(&quot;{id}&quot;)]
      public async Task&lt;ActionResult&lt;Phonenumbertype&gt;&gt; GetPhonenumbertype(short id)
      {
          var phonenumbertype = await _context.Phonenumbertypes.FindAsync(id);
  
          if (phonenumbertype == null)
          {
              return NotFound();
          }
  
          return phonenumbertype;
      }
  
      // PUT: api/Phonenumbertypes/5
      // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
      [HttpPut(&quot;{id}&quot;)]
      public async Task&lt;IActionResult&gt; PutPhonenumbertype(short id, Phonenumbertype phonenumbertype)
      {
          if (id != phonenumbertype.PhtId)
          {
              return BadRequest();
          }
  
          _context.Entry(phonenumbertype).State = EntityState.Modified;
  
          try
          {
              await _context.SaveChangesAsync();
          }
          catch (DbUpdateConcurrencyException)
          {
              if (!PhonenumbertypeExists(id))
              {
                  return NotFound();
              }
              else
              {
                  throw;
              }
          }
  
          return NoContent();
      }
  
      // POST: api/Phonenumbertypes
      // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
      [HttpPost]
      public async Task&lt;ActionResult&lt;Phonenumbertype&gt;&gt; PostPhonenumbertype(Phonenumbertype phonenumbertype)
      {
          _context.Phonenumbertypes.Add(phonenumbertype);
          try
          {
              await _context.SaveChangesAsync();
          }
          catch (DbUpdateException)
          {
              if (PhonenumbertypeExists(phonenumbertype.PhtId))
              {
                  return Conflict();
              }
              else
              {
                  throw;
              }
          }
  
          return CreatedAtAction(&quot;GetPhonenumbertype&quot;, new { id = phonenumbertype.PhtId }, phonenumbertype);
      }
  
      // DELETE: api/Phonenumbertypes/5
      [HttpDelete(&quot;{id}&quot;)]
      public async Task&lt;IActionResult&gt; DeletePhonenumbertype(short id)
      {
          var phonenumbertype = await _context.Phonenumbertypes.FindAsync(id);
          if (phonenumbertype == null)
          {
              return NotFound();
          }
  
          _context.Phonenumbertypes.Remove(phonenumbertype);
          await _context.SaveChangesAsync();
  
          return NoContent();
      }
  
      private bool PhonenumbertypeExists(short id)
      {
          return _context.Phonenumbertypes.Any(e =&gt; e.PhtId == id);
      }
     }
  }
  </code></pre>
  <p>I run my web API, Swagger starts, select my API (Try Out) and get Error: response status is 500 and the following body:</p>
  <p><em><strong>System.InvalidOperationException: Unable to resolve service for type 'WebAppTest.Models.JMSContext' while attempting to activate 'WebAppTest.Controllers.PhonenumbertypesController'.
  at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
  at lambda_method9(Closure , IServiceProvider , Object[] )....</strong></em></p>
  <p>This is all auto-generated code so my expectations was that is would work and I would expand my code from there ... this is just a basic test.</p>
  <p>It was able to get the results I wanted by removing all the auto-generated code in controller class ... BUT, I'm trying to figure out why the auto-generated code doesn't work?</p>