我的存储过程返回一个连接表,如:
Employee Department
-------------------
John IT
Bob IT
Rob IT
Jane Sales
Mary Sales
我有EF生成的适当实体:
class Employee
{
public int Id {get;set;}
public string Name{get;set;}
public int DepartmentId{get;set;}
public Department Deparment{get;set;}
}
class Department
{
public int Id {get;set;}
public string Name{get;set;}
public ICollection<Employees> Employees{get;set;}
}
我执行这样的存储过程:
Database.SqlQuery<Department>("exec spGetDepartments").ToList();
结果是一系列部门:
IT
IT
IT
Sales
Sales
每个人都有空的员工名单。
我可以让2个部门实体拥有相应员工的名单吗?
示例已经过简化,但业务需求是使用存储过程,因为在LINQ中存在难以复制的复杂逻辑。
答案 0 :(得分:1)
You cannot employ the navigation properties on stored procedures. Note the stored procedure doesn't actually employ a context method call, but an actual query execution.
You have to use the DBcontext, to accomplish the task you require. Stored procedures aren't "composable". The "Lazy loading" or its variants are only available via a context.
You could in theory achieve a read statement in a generic repository pattern, if you absolutely wanted to. It might even be efficient under some circumstances.
But in that case, your resultset on the stored procedure, would always have to hold your entire row set. The behavior would be quite complicated. I wouldn't actually advise it. It might be possible, but smart? decidedly not.