在我的MVC应用程序ApplicationUser和Employee类有1-1的关系:
public class ApplicationUser : IdentityUser
{
public Employee Employee { get; set; }
}
public class Employee
{
[Key]
public virtual ApplicationUser ApplicationUser { get; set; }
public virtual string Name { get; set; }
}
在一个公共静态类
我有以下几种方法:
public static ApplicationUser GetCurrentApplicationUser(string userName)
{
using (DbContext db = new DbContext())
{
return db.Users.FirstOrDefault(u => u.UserName.Equals(userName));
}
}
public static Employee GetEmployeeByApplicationUser(string userName)
{
using (DbContext db = new DbContext())
{
return db.Employees.SingleOrDefault(e => e.ApplicationUser == GetCurrentApplicationUser(userName));
}
}
你可以看到第二个方法消耗的第一个方法。 但我发现了以下错误:
System.NotSupportedException was unhandled by user code
Message=LINQ to Entities does not recognize the method 'GetCurrentApplicationUser(System.String)' method, and this method cannot be translated into a store expression.
但是,如果我我的第一种方法里面的内部代码粘贴到我的第二个方法如下图所示,它工作正常。
return db.Employees.SingleOrDefault(e => e.ApplicationUser == db.Users.FirstOrDefault(u => u.UserName.Equals(userName)));
什么我在这里失踪? 为什么我得到这个错误?
谢谢!