【发布时间】:2014-10-09 20:54:35
【问题描述】:
在我的 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)));
我在这里缺少什么? 为什么会出现此错误?
谢谢!
【问题讨论】:
标签: c# linq linq-to-entities asp.net-mvc-5 entity-framework-6