I'm trying to do my first API with ASP.NET Core 6 and Entity Framework Core.
Basically I have a model class:
namespace SistemaDeTarefas.Models
{
public class UsuarioModel
{
public int Id { get; set; }
public string? Nome { get; set; }
public string? Email { get; set;}
}
}
An interface
using SistemaDeTarefas.Models;
namespace SistemaDeTarefas.Repositorios.Interfaces
{
public interface IUsuarioRepositorio
{
Task<List<UsuarioModel>> BuscarTodosUsuarios();
Task<UsuarioModel> BuscarId(int id);
Task<UsuarioModel> Adicionar(UsuarioModel Usuario);
Task<UsuarioModel> Atualizar(UsuarioModel Usuario, int id);
Task<bool> Apagar(int id);
}
}
And a repository
using Microsoft.EntityFrameworkCore;
using SistemaDeTarefas.Data;
using SistemaDeTarefas.Models;
using SistemaDeTarefas.Repositorios.Interfaces;
namespace SistemaDeTarefas.Repositorios
{
public class UsuarioRepositorio : IUsuarioRepositorio
{
private readonly SistemaTarefasDBContext _dbContext;
public UsuarioRepositorio(SistemaTarefasDBContext sistemaTarefasDbContext)
{
_dbContext = sistemaTarefasDbContext;
}
public async Task<UsuarioModel> Adicionar(UsuarioModel usuario)
{
return await _dbContext.Usuarios.Add(usuario);
}
public Task<bool> Apagar(int id)
{
throw new NotImplementedException();
}
public Task<UsuarioModel> Atualizar(UsuarioModel Usuario, int id)
{
throw new NotImplementedException();
}
public async Task<UsuarioModel> BuscarId(int id)
{
return await _dbContext.Usuarios.FirstOrDefaultAsync(x => x.Id == id);
}
public async Task<List<UsuarioModel>> BuscarTodosUsuarios()
{
return await _dbContext.Usuarios.ToListAsync();
}
}
}
In both methods Adicionar() and BUscarTodosUsuarios(), the return gets highlighted and Visual Studio warns
Can't convert from SistemaDeTarefas.Models.UsuarioModel to UsuarioModel
That makes no sense, since it's like it can't convert from model to path.model
I was trying to implement methods for this entity crud, but then I get the error described
UsuarioModel?DbSet<Usario>, not aDbSet<UsarioModel>. If it does happen to be a UsarioModel, right-click on the UsarioModel inside the arrow brackets and Go to Definition. It will take you to a different class than the SistemaDeTarefas.Models.UsuarioModel.