Dzień dobry,
postanowiłem pobawić się troche z Entiny Framework Core i utworzyłem sobie w Xampie testową bazę danych.
Następnie w Visual Studio 2022 utworzyłem sobie projekt i zainstalowałem niezbędne komponenty Entiny Framework Core dla MySQL.
Niestety po uruchomieniu aplikacji (WebAPI) i wywołania adresu http://localhost:5000/ksiegowosc/getlist
zamiast danych otrzymuje taki błąd:
An unhandled exception occurred while processing the request.
InvalidOperationException: Unable to resolve service for type 'WebAPI.AppDbContext' while attempting to activate 'WebAPI.Controllers.KsiegowoscController'.
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)
Stack Query Cookies Headers Routing
InvalidOperationException: Unable to resolve service for type 'WebAPI.AppDbContext' while attempting to activate 'WebAPI.Controllers.KsiegowoscController'.
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)
lambda_method2(Closure , IServiceProvider , object[] )
Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider+<>c__DisplayClass7_0.<CreateActivator>b__0(ControllerContext controllerContext)
Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider+<>c__DisplayClass6_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Poniżej zamieszczam zawartość poszczególnych plików:
ksiegowosc.cs
(Controller)
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using WebAPI.Models;
namespace WebAPI.Controllers
{
[ApiController]
[Route("[controller]")]
public class KsiegowoscController : Controller
{
private readonly AppDbContext _dbContext;
public KsiegowoscController(AppDbContext dbContext)
{
_dbContext = dbContext;
}
[HttpGet]
[Route("getlist")]
public IActionResult Get()
{
var result = _dbContext.rachunki.ToList();
return Ok(result);
}
}
}
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Urls": "http://localhost:5000",
"ConnectionStrings": {
"DataBase": "server=localhost.;user=ksiegowosc; password=ksiegowosc; database=ksiegowosc; port=3306"
}
}
rachunki.cs
(Models)
namespace WebAPI.Models
{
public class Rachunki
{
public int id { get; set; }
public DateTime data_trans { get; set; }
public decimal kwota { get; set; }
public int miesiac { get; set; }
public string opis { get; set; }
public int przenoszona_kwota { get; set; }
public int rodzaj_rach { get; set; }
public int rok { get; set; }
}
}
AppDBContex.cs
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using WebAPI.Models;
namespace WebAPI
{
using Microsoft.EntityFrameworkCore;
public class AppDbContext : DbContext
{
public DbSet<Rachunki> rachunki { get; set; }
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
}
}
Zdjęcie struktury bazy rachunki
Zainstalowane zależności: