views:

233

answers:

1

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?

A: 

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.

Jappie
Thank you. The solution provided by EFExtensions is not what I want. It gives you mutiple result sets from SP, like you have more than one select in your SP. But what I want is get the entityset with all its lookup data. Like Include for ObjectSet. For example, Employee may include 3 lookup table foreign keys: DepartmentID, RoleID, GenderID. I
KentZhou
What you want is not possible with Entity Framework. Like I said, a stored procedure function import will only return one entity type, and cannot include it's lookup data.
Jappie
Thanks for your information. Find out one solution: Loading lookup manually in code.
KentZhou