I have already given an introduction to Entity Framework on Day 1 to view the Day 1 Article click here.
Creating the stored procedure
Executing Stored Procedure using Entity Framework
First, write a Stored Procedure in your database as in the following.
This procedure takes one input parameter @MerchantID and returns the Name and Address of the respective merchant ID
Adding entity model to project
To add an entity model to your project check Entity Framework on Day1.
You can select the procedure at the time of creation of the entity model.
Otherwise, you can add it after the creation of the project.
Use the following procedure to create a sample.
- Double-click on Day2Model.dmx.
![Solution explorer]()
- Then you will get an edmx diagram screen. Right-click on that screen and select the model browser.
![Model Browser]()
- Then right-click on Stored Procedure of “MyModel.store” and select “Update Model from Database”
![Update model from database]()
- Check your procedure and finish, then automatically your procedure execution function will be created. You can change the return type also after the addition of the procedure
![Usp get merchant details]()
Now your model browser looks like
![Usp get merchant details]()
- Entity type: Merchant - considered as simple Merchant Table
- Complex type: USP_GetMerchantDetails_Result - Return Type of Stored Procedure, we can use this when we want to return multiple table columns, like M.MerchantName and p.ProductName.
- Function imports: USP_GetProductDetails - execute function
- Same in Stored procedure: USP_GetProductDetails - you will get the option on this procedure to change and add functions
Check your code generated in Day2Model.Designer.cs
Calling the auto-generated procedure function
So now call the auto-generated procedure function and execute the Stored Procedure.
Complex Return Type
Debug result
![Debug result]()
Entity Return Type
To add a new function for a Stored Procedure click on
![Add function import]()
Now click the Entity dropdown and select the respective Entity.
![Add function import]()
The following is the code to call this function
Every time check your generated code in the designer.cs file.
The difference between complex type functions and entity type functions to execute a procedure.
Everything is the same except the return type
The following is the auto-generated code of the entity type and complex type
![Summary]()
Paging in entity framework
Use custom paging to increase performance.
Just mention the current page number, page size, and entity to return the current page record
Sorting in entity framework
Just mention the current column Name, order by state, page number, page size, and entity to return the current page record with sort
Please find the attached source code and enjoy Entity Framework.
Thanks for reading!!!
Go to part 3