Reputation:
I was doing a simple HTTP GET
operation, I will display person data as JSON
after hitting my url
at first, all worked
Working code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Apitest4.Models;
namespace Apitest4.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PeopleController : ControllerBase
{
[HttpGet]
public IEnumerable<Person> Get()
{
using (var context = new ModelContext())
{
return context.People.ToList();
}
}
}
}
The above code you are seeing is a slightly modified version of WeatherController
(Visual Studio 2019), which I was using to make my own PeopleController
. This code gives me proper API response I expected
Then I wanted to do the same using an automated approach, since I have to cover a lot of tables
[Vs Code: 2019]
[Controllers(Right Click)>Add>New Sacffolded Item>Api Controller with Actions using Entity Framework]
Auto-generated Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Apitest4.Models;
namespace Apitest4.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PeopleController : ControllerBase
{
private readonly ModelContext _context;
public PeopleController(ModelContext context)
{
_context = context;
}
// GET: api/People
[HttpGet]
public async Task<ActionResult<IEnumerable<Person>>> GetPeople()
{
return await _context.People.ToListAsync();
}
}
}
This code is givine me this error:
InvalidOperationException: Unable to resolve service for type 'Apitest4.Models.ModelContext' while attempting to activate 'Apitest4.Controllers.PeopleController'.
Full error message:
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Apitest4
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
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.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
Though I am new to .net, I just don't get it, Why it is not getting context properly? I didn't change any import. Please point my mistakes.
TIA
Upvotes: 0
Views: 407
Reputation: 36735
Be sure register your DbContext in Startup.cs like below:
services.AddDbContext<ModelContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
Refrence:
Update:
store the connection string in appsettings.json:
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=DatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true"
}
Upvotes: 1