views:

614

answers:

2

Hi everyone.

I want to implement a search logic that I have seen and use. It works like this:

There is stored procedure that is loaded in entity framework and it is used as a method in C#, it takes parameters in order to perform search. That method is returning list of views (DB View). The views in list have just some of the properties that are needed to be shown in the gird. For example I am searching for users by some parameters and result is list of users. Instead of returning list of User object I am using a vUser view that contains only properties that will be listed on gird. For example ID, Name, and Surname. But still data is correct because stored procedure search is done against table Users in database.

View is also loaded as C# object in entity framework. I am using SQL Server 2005.

How I can implement this?

thank you very much.

+1  A: 

Yes you can do this.

Unfortunately in EF 3.5 you need to create an EntityType (that has just the columns you project in the sproc) so that you can create a FunctionImport that exposes the results of the SPROC.

So steps:

  1. Make sure you include the Store Proc when creating the model (orwhen refreshing the model from the database)
  2. Create an EntityType that exactly matches the shape returned by the SPROC
  3. Create a FunctionImport that using that Sproc and say it returns the EntityType you created in (2)

In EF 4.0 you can skip step (2) because the EF tooling automates this process.

See this post from Julie for more background

Hope this helps

Alex

Alex James
What will that function return? Will that be a list of Views that have the same properties that I am selecting in SQL code in stored procedure?
SonOfOmer
Well if when you say list of views you mean list of rows from the view, then yes.
Alex James
yes yes, thanks a lot
SonOfOmer
hi again I have done all in folowing steps:Load store procedure and view into edmx file and in model browser on folder sunction imports, right click add Function import and chose the stored procedure gave it name and chose the view among entities for return type.When I try to do search I get EntityCommandExecutionException exception with message that "The data reader is incompatible with the specified 'vUser'. A member of the type, 'Name', does not have a corresponding column in the data reader with the same name.""Name" is on the same place in parameters list in function as in SPROC.
SonOfOmer
Your Entity Type needs to have the same named properties as the reader. i.e.if you have Entity{ID,Name,Surname} then when you call the stored proc directly the resulting reader should have ID in column 0, Name in column 1 and Surname in column 2 etc. This is not about matching parameters (i.e. the things you pass to the Sproc).
Alex James
A: 

This is not answer just extension of the question.

In order to do all the job in entity framework layer I must have valid store procedure that does search of 'users' in this case.

I did some search on Google and spot that there are two ways to return data with store procedure. One with out parameter and one with select inside brackets @parameter( SELECT goes here )

What is the best way to write store procedure that can be used as source for import function in entity framework that will return view that contains only some attributes of the user. For example Id, Name, Surname, Phone and Address because they will be shown in a grid as a result of search.

Thanks a lot everyone sorry but this is new for me

SonOfOmer
Don't do this. You can edit the original question to include more information.
emzero