0

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

6
  • Do you have 2 classes called UsuarioModel? Commented Dec 2, 2023 at 21:30
  • Nope, only this one Commented Dec 2, 2023 at 22:05
  • 2
    Please check closely, that is the only reason you can get this error. Commented Dec 2, 2023 at 22:21
  • 1
    If you used EF to scafold a database then it would have created an entity definition. The easiest way to find it is to go to your DbContext.Usarious, which will be a DbSet<Usario>, not a DbSet<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. Commented Dec 2, 2023 at 23:48
  • Side note: another tip, remove this repository layer from your application. As you see it does nothing but wrapping a DbSet. It's totally redundant. Commented Dec 3, 2023 at 12:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.