EF 和域服务上存储过程的 ObjectSet 和 ResultSet
在 EF 4 中,默认的 ObjectSet 可用于每个实体。例如,我有表 Employee,生成实体模型后,EF 将在 Employee 上创建 ObjectSet。 那么当使用wcf ria服务时,默认查询将类似于:
public IQueryable GetEmployee() { 返回 this.ObjectContext.Employees; 使用
objectSet,我可以对结果应用 include,如下所示:
return this.ObjectContext.Employees.Include("Department");
然后我创建一个存储过程(例如 MySearchForEmployee)并将其作为函数导入。结果映射到实体 Employee。调用该函数时,结果将是ResultSet,而不是ObjectSet。
我想通过调用存储过程来为域服务提供类似的方法,例如:
public IQueryable<Employeer> GetMySearch(string keyword)
{
return this.ObjectContext.MySearchForEmployee(keyword).Include("Department");
}
但我不能,因为上面的代码事件无法通过语法检查。
我尝试以下方法来转换结果类型:
var results = this.ObjectContext.MySearchForEmployee(keyword);
var objsets = (ObjectSet<Employee>) results;
然后我收到错误: 无法将类型“System.Data.Objects.ObjectResult”转换为“System.Data.Objects.ObjectSet”
如何实现此请求?
In EF 4, the default ObjectSet is available for each entity. For example, I have table Employee, after gererated Entity Model, EF will create ObjectSet on Employee.
Then when using wcf ria service, the default query will be like:
public IQueryable GetEmployee()
{
return this.ObjectContext.Employees;
}
With objectSet, I can apply include on the result like:
return this.ObjectContext.Employees.Include("Department");
Then I create a stored procedure say MySearchForEmployee and import it as function. the result is mapped to entity Employee. When call the function, the result will be ResultSet, not ObjectSet.
I want to similar method available for domain service by call the stored procedure like:
public IQueryable<Employeer> GetMySearch(string keyword)
{
return this.ObjectContext.MySearchForEmployee(keyword).Include("Department");
}
But I can't becuase above code event can't pass syntax checking.
I tried following way to convet the result type:
var results = this.ObjectContext.MySearchForEmployee(keyword);
var objsets = (ObjectSet<Employee>) results;
then I got error as:
Cannot convert type 'System.Data.Objects.ObjectResult' to 'System.Data.Objects.ObjectSet'
How to implement this request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有理由使用 ObjectSet,因为您不能在存储过程的一个查询中包含多个实体集。实体框架不支持此功能。
您可以尝试 EFExtensions 项目,它具有从一个查询加载多个实体集的扩展。
There is no reason to use an ObjectSet, because you cannot include multiple entity sets in one query from a stored procedure. This is not supported in the Entity Framework.
You could try the EFExtensions project, it has extensions to load multiple entity sets from one query.