In my MVC application ApplicationUser and Employee classes have 1-1 relationship:
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; }
}
In a public static class
I have following methods:
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));
}
}
as you can see second method is consuming the first method. But I'm getting the following error:
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.
But if I paste the inner code inside my first method to my second method like below, it works fine.
return db.Employees.SingleOrDefault(e => e.ApplicationUser == db.Users.FirstOrDefault(u => u.UserName.Equals(userName)));
What I'm missing here? Why I'm getting this error?
Thanks!