Login with Asp.net 4.5 (Entity Framework)?

0

How can I make a Select using Entity Framework , and check if the registry exists?

Example:

txtlogin.text
txtsenha.text

Login table.

  • user field
  • password field

valida() class created to execute the command and selecting valida() it will check if usuario and senha exists via Entity Framework

    
asked by anonymous 23.03.2017 / 16:22

2 answers

1

I use this way:

using(var context = new SeuContexto()){
      var Usuario = context.login.firstOrDefault(x=> x.usuario == txtlogin.text && x.senha == txtsenha.text)
      if(Usuario != null)
      {
            //Existe o usuário
      }else
      {
            //Se voltar nulo, não existe o usuário com esta senha
      }
}
    
23.03.2017 / 18:24
0

You can use the Any() method of LINQ. It returns true if there is any ( any in English) record. If it does not exist, it returns false . Ex:

if(contexto.Entidade.Any(x => x.Login == txtLogin.Text && x.Senha == txtSenha.Text))
{
    //existe
}
else
{
    //não existe
}

The documentation for the Any() method you can see here >.

    
23.03.2017 / 18:31