Practical Introduction To Entity Framework: Day 2

I have already given an introduction to Entity Framework in Day 1; to view the Day 1 Article click here.

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.

  1. Create PROCEDURE [dbo].[USP_GetMerchantDetails] --1  
  2. (  
  3.    @MerchantID int  
  4. )  
  5. AS  
  6.     BEGIN  
  7.         select Name, Address from Merchant where id=@MerchantID  
  8.     END  

To add an entity model to your project check Entity Framework in 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.

  1. Double-click on Day2Model.dmx.

    Model

  2. Then you will get a edmx diagram screen. Right-click on the that screen and select model browser.

    model browser

  3. Then right-click on Stored Procedure of “MyModel.store” and select “Update Model from Database”.

    Update Model

  4. Check your procedure and finish, then automatically your procedure execution function will be created. You can change the return type also after addition of the procedure.

    procedure

Now your model browser looks like:

model browser

Entity type: Merchant - consider 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 procudre to change and add functions

Check your code generated in Day2Model.Designer.cs.

  1. public ObjectResult<USP_GetMerchantDetails_Result> USP_GetMerchantDetails(Nullable<global::System.Int32> merchantID)  
  2. {  
  3.     ObjectParameter merchantIDParameter;  
  4.     if (merchantID.HasValue)  
  5.     {  
  6.         merchantIDParameter = new ObjectParameter("MerchantID", merchantID);  
  7.     }  
  8.     else  
  9.     {  
  10.                 merchantIDParameter = new ObjectParameter("MerchantID"typeof(global::System.Int32));  
  11.             }  
  12.       
  13.             return base.ExecuteFunction<USP_GetMerchantDetails_Result>("USP_GetMerchantDetails", merchantIDParameter);  
  14.         } 

So now call the auto-generated procedure function and execute the Stored Procedure.

Complex Return Type

  1. MyModel.MyEntities Obj = new MyModel.MyEntities();  
  2. var qq = Obj.USP_GetMerchantDetails(1); //1 is merchantid 

Debug result:

debug result

Entity Return Type

To add a new function for a Stored Procedure click on:

Entity Return Type

Now click the Entity dropdown and select respective Entity.

respective Entity

The following is the code to call this function:

  1. MyModel.MyEntities Obj = new MyModel.MyEntities();  
  2. var Result = Obj.USP_GetMerchantDetails_Entity(1); 

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.

  1. Everything is the same except return type

The following is the auto-generated code of the entity type and complex type:

complex type

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.
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.    MyModel.MyEntities Obj = new MyModel.MyEntities();  
  4.    ObjectSet<Merchant> MerchantList = Obj.Merchants;  
  5.    var CurrentRecords = GetCurrentPageRecords(3, 2, MerchantList);  
  6. }  
  7. public IEnumerable<Merchant> GetCurrentPageRecords(int CurrentPageNumber, int PageSize, ObjectSet<Merchant> MerchantList)  
  8. {  
  9.    return MerchantList.OrderBy(s => s.Id).Skip(PageSize * CurrentPageNumber - 1).Take(PageSize).Select(a => a);  

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.

  1. var CurrentRecordswithsort = GetSortingRecords("name_desc",3, 2, MerchantList);  
  2.   
  3. public IEnumerable<Merchant> GetSortingRecords(string ColumnNameWithAscDesc,int CurrentPageNumber, int PageSize ,ObjectSet<Merchant> MerchantList)  
  4.     {  
  5.         switch (ColumnNameWithAscDesc.ToLower())  
  6.         {  
  7.             case "id":  
  8.                 return MerchantList.OrderBy(s => s.Id).Skip(PageSize * (CurrentPageNumber - 1)).Take(PageSize).Select(a => a);  
  9.              case "id_desc":  
  10.                 return MerchantList.OrderByDescending(s => s.Id).Skip(PageSize * (CurrentPageNumber - 1)).Take(PageSize).Select(a => a);  
  11.             case "name":  
  12.                 return MerchantList.OrderByDescending(s => s.Name).Skip(PageSize * (CurrentPageNumber - 1)).Take(PageSize).Select(a => a);  
  13.             case "name_desc":  
  14.                 return MerchantList.OrderByDescending(s => s.Name).Skip(PageSize * (CurrentPageNumber - 1)).Take(PageSize).Select(a => a);  
  15.             default:  
  16.                 return MerchantList.OrderBy(s => s.Id).Skip(PageSize * CurrentPageNumber - 1).Take(PageSize).Select(a => a);  
  17.         }  
  18.   
  19.     } 

Please find the attached source code and enjoy Entity Framework.

Thanks for reading!!!

Up Next
    Ebook Download
    View all
    Learn
    View all