Tuesday, August 10, 2010

Using a Stored Procedure in Entity Framework 4

If you have worked with LINQ and added stored procedures to it, it would seem obvious to be able to it the same way in Entity framework.

In LINQ all you had to do is to drag the stored procedure from your Server Explorer respective data connection on your model diagram.

In entity framework 4, there are few steps and they are different.

1. On your entity diagram in visual studio, right click and choose Update Model from database.

2. Add your stored procedure from the wizard. What it does, it adds the proc to your diagram. Its not visible in .Net code yet.
If you go to Model Browser > {EntityModel}.Store > Stored Procedures, it should be visible in there now

3. Right click, Add > Function Import. Complete this according to your requirement, name of the proc you want to see in .Net, which stored proc it maps to and what is the collection it returns etc.

After step 3 you can access access the proc using an instance of context.

For more visual representation of the steps, please visit WebLog of Ken Cox

No comments: