2014-01-31 185 views
0

挣扎新手:(问题与登录

实体没有获取用户..

登录控制器

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

登录型号

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

    } 
} 

异常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. 
+0

如何使用ID获取用户? – JRU

+0

你确定用户在那?如果你得到所有的用户,你是否看到了用户正在测试的'LoginID'? –

+0

@AndreiV你是否指一个实体? – JRU

回答

3

关于你例外,你可以尝试获得SingleOrDefault方法外的相应的散列:

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

    } 
} 

编辑:由于在评论中所建议的Rezoan的更多信息,可以通过阅读Entity Framework can't run your C# code as part of its query发现。

+0

非常感谢,如果你让我知道为什么它不被我的代码接受:) – JRU

+1

我不是lambda表达式的专家,但它可能与事实有关,在你的lambda表达式中,有另一个“实例”,即关键字“this”不是指您的“用户”实例。也许有人可以提供更好的解释。 –

+1

@JRU实体框架不能实际运行您的C#代码作为其查询的一部分。看到StriplingWarrior的答案http://stackoverflow.com/questions/7259567/linq-to-entities-does-not-recognize-the-method – Rezoan

0

您应该将SetPassword方法的结果存储在本地varoable中,并在lambda表达式中使用varoable。异常很明显,setpassword方法在数据库端不可用。