开发者

Is this a recommended way to use a Stored Procedure with Entity Framework 4?

My code is doing two round trips to the database because I'm not sure if this is the correct way to create a collection of my POCO's where the first round trip hits a stored procedure because of some specific SQL code.

Scenario

A user enters some AutoComplete search query into the UI. My code hits a stored procedure (which is taking advantage of F*ull Text Search* - hence the reason I'm using a Stored Procedure) and returns all the distinct primary keys of the results. These go back to the code (my IRepository class) and I THEN use EF to retrieve all the results, for these stored procedure result.

Firstly, I don't know how to do that in Linq to Entities : for a collection of Id's, retrieve all the Foo entities.

Secondly, i'm doing TWO round-trips to the database. Why? Because I'm not sure how I can retrieve the rich results in the first round trip. The entity consists of a few POCO classes and has two ICollection properties also, etc...

Is this the correct way I should be using a stored procedure and then retrieving the rich, populated entities.

I'll quickly create a dummy class diagram to help with any answers.

public class Person
{
    string Name;
    int Age;
    ICollection<string> NickNames;
    ICollection<Foo> DonnoSomethingE开发者_StackOverflow社区lses;
}

Thoughts?


Well, the two roundtrips sound like an avoidable problem - couldn't you do something like:

WITH FullTextResults AS
(
   SELECT ID, ...... --- do your full text search here
)
SELECT
   (list of fields) 
FROM
   dbo.YourDataTable t
INNER JOIN
   FullTextResults ftr ON t.ID = ftr.ID  -- join on primary key
WHERE
   .....  -- possibly limit results

and just return the output of this CTE (Common Table Expression) from your stored proc?? That would then return a data set with rows and columns from your actual data table, and EF could materialize your Person classes from that.....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜