Log in with Asp.net 4.5 (Entity Framework)?

Asked

Viewed 192 times

0

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

Example:

txtlogin.text
txtsenha.text

Login table.

  • user camp
  • password field

valida() class created to execute the command and by selecting the valida() he will check if there is the usuario and senha via Entity Framework

  • Can’t you validate before you send it to Entity? Anyway, where’s the code, can you put it? Can’t you use Data Annotations? https://msdn.microsoft.com/en-us/library/gg193959(v=vs.113). aspx

2 answers

1


I wear it like this:

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
      }
}

0

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

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

Documentation of the method Any() you can see here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.