SqlQuery into a [NotMapped] field?
I had the same trouble using stored procedures to do selects with calculated fields. I created a view model that looks exactly like my entity without the db annotations. Then after I call my stored procedure using the view model I select into my entity. So, using your example above:
public class EmployeeVM
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string CustomerName { get; set; }
}
Then you can call:
public List<Employee> GetEmployees()
{
using (MyContext context = new MyContext())
{
return context.Database.SqlQuery<EmployeeVM>("select E.EmployeeId, E.EmployeeName,
C.CustomerName from Employee E left join Customer C on E.CustomerId = C.CustomerId")
.Select(x=> new Employee(){
EmployeeId = x.EmployeeId,
EmployeeName = x.EmployeeName,
CustomerName = x.CustomerName
}).ToList();
}
}
Hope this helps.