I did a test project, called quotation to use the Web API and Entity. Well, I've been to my web.config and created my connection string like this:
<add name="CotacaoContext" connectionString="Data Source=Minha_Maquina\Instancia; Initial Catalog=Cotacao; User Id=sa; Password=d123" providerName="System.Data.SqlClient" />
This provider is there, because before I had made a mistake of his lack, so I put it. In my controller I have made a method to get the users of the DB and it is giving a stick. This is my method in Controller (all code).
public class UsuarioController : ApiController
{
private CotacaoContext contexto = new CotacaoContext("CotacaoContext");
[AcceptVerbs("Get")]
public IEnumerable<Usuarios> GetUsuarios()
{
return contexto.Usuario.AsEnumerable();
}
public Usuarios GetUsuarioById(int id)
{
Usuarios usuario = contexto.Usuario.Find(id);
if(usuario == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return usuario;
}
protected override void Dispose(bool disposing)
{
contexto.Dispose();
base.Dispose(disposing);
}
}
This is my Context class that will create the DbSets and connect to the database.
public class CotacaoContext : DbContext
{
public CotacaoContext(string connectionString)
: base(connectionString)
{
}
public DbSet<Usuarios> Usuario { get; set; }
public DbSet<Login> Login { get; set; }
}
And this is my model that abstracts the table from the database.
public class Usuarios
{
public int Id { get; set; }
public int Tipo_Usuario { get; set; }
public string NMUsuario { get; set; }
public string Senha { get; set; }
}
An important note. There is 6 record in the table that I put in the hand, and there is a table that does a foreign key in the user table, but since I am testing the connection first, I have not put it in my model yet. Below is a print at the time of the debug.
SeethatwhenIputthemouseinthecontextvariable,6itemsappear,butwithaquestionmark(mytablehas6records).Ihavebeensincelastnightontheinternet,totrytofindoutwhy,butlackalittlemoreknowledge(that'swhatI'mlookingfor).I'mwaiting.
Edit1-Animportantpoint.Theattribute[AcceptVerbs("Get")]
I put later, in the attempt to hit.
Edit2 - There came a light. Do I need to do anything in the api web routes file?
Edit3 - When I run both in the browser and in Postman, this is the result: []