0

Struggling Newbie :(

Entity not fetching the User..

Login Controller

[HttpPost]
public ActionResult LoginForm(string user_NA, string user_pwd)
{
    User u = new User();
    u.LogIn(user_NA, user_pwd);
    return RedirectToAction("Index");
}

Login Model

public bool LogIn(string userID, string password)
{
    using (Entity.xcLEntities en = new xcLEntities())
    {
        try
        {
            var user = en.Users.SingleOrDefault(x => x.LoginID == userID && x.PasswordHash == this.SetPassword(password));
            if (user != null && password != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception e)
        {
            throw e;
        }

    }
}

Exception shoing

An exception of type 'System.NotSupportedException' occurred in AML.Web.dll but was not handled in user code

Additional information: LINQ to Entities does not recognize the method 'System.String SetPassword(System.String)' method, and this method cannot be translated into a store expression.
JRU
  • 327
  • 2
  • 9
  • 18

2 Answers2

3

Regarding your exception, you could try getting the corresponding hash outside the SingleOrDefault method:

public bool LogIn(string userID, string password)
{
    using (Entity.AMLEntities en = new AMLEntities())
    {
        try
        {
            string hashed = this.SetPassword(password);
            var user = en.Users.SingleOrDefault(x => x.LoginID == userID && x.PasswordHash == hashed);
            if (user != null && password != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception e)
        {
            throw e;
        }

    }
}

EDIT: As suggested by Rezoan in the comments, more information can be found by reading Entity Framework can't run your C# code as part of its query.

Community
  • 1
  • 1
Andrei V
  • 7,306
  • 6
  • 44
  • 64
  • Thanks... It would be very grateful if u let me know why it's not accepted in my code :) – JRU Jan 31 '14 at 09:35
  • 1
    I'm no expert in lambda expressions but it might have something to do with the fact that inside your lambda expression, there's another "instance", i.e. the keyword `this` is not referring your `User` instance. Maybe someone can provide a better explanation. – Andrei V Jan 31 '14 at 09:37
  • 1
    @JRU Entity Framework can't actually run your C# code as part of its query. see the answer of StriplingWarrior on http://stackoverflow.com/questions/7259567/linq-to-entities-does-not-recognize-the-method – Rezoan Jan 31 '14 at 09:43
0

You should store the result of the SetPassword methon in a local varoable and use the varoable in the lambda expression. The exception is clearly state that the setpassword method is not avaliable on the db side.

Péter
  • 2,161
  • 3
  • 21
  • 30