开发者

Stored procedure search using entity framework

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.


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 stored procedure) so that you can create a FunctionImport that exposes the results of the stored procedure.

So steps:

  1. Make sure you include the stored procedure when creating the model (or when refreshing the model from the database)

  2. Create an EntityType that exactly matches the shape returned by the stored procedure

  3. Create a FunctionImport that using that stored procedure 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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜