简体   繁体   中英

How to retrieve a specific record using the entity framework

I have a table users which stores three values ie username ,the password and the member type .I want to ensure that the username stored in the table is unique and hence there is a primary key on it.However I want to do the validation at the client side itself instead of sending a insert request to the database and then catching the error .

So how can I retrieve the single record from the database which would contain both username and password so that I can use it for the comparison purposes in the code and then throw a validation error if needed.

My manager wants me to use stored procedures so inputs along those lines would be great

public static bool IsUserExists(string userName, string hashedPassword)
{
  bool result = false;

  using (MyEntities entityContext = new MyEntities())
  {
    result = (entityContext.User.Count(u => u.username == userName && 
                                            u.password == hashedPassword) == 1);
  }

  return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM