Practical Introduction To Entity Framework: Day 3

The following are my previous articles on the fundamentals of Entity Framework:

    Before starting with the fundamentals, create a normal Stored Procedure so we can execute that Stored Procedure in the following scenarios.

    Stored Procedure

    1. Create PROCEDURE [dbo].[USP_GetCustomerByID]  
    2. (  
    3. @CustomerID int  
    4. )  
    5. AS  
    6. BEGIN  
    7. SELECT * from Customer where id=@CustomerID  
    8. END  
    The Stored Procedure is now created and I assume you understand all the basics of the Entity Framework.

    So, Let's start; it's very easy.
    1. How to execute an raw SQL query

      The following is another way to execute a SQL query without calling entity functions:
      1. using (var ObjEntity = new CRMModel.CRMEntities())  
      2. {   
      3. var GetAllCustomer = ObjEntity.ExecuteStoreQuery<Customer>("select * from customer");  
      4. }  
    2. The following executes a Stored Procedure without mapping the Stored Procedure function in edmx; pass a parameter in the same as we pass one in SQL Managmet Studio, so you will get an appropriate procedure output.
      1. using (var ObjEntity = new CRMModel.CRMEntities())  
      2. {   
      3. var Cuetomer1 = ObjEntity.ExecuteStoreQuery<Customer>("USP_GetCustomerByID 1");   
      4. }  
    3. The following executes Insert/Update/Delete Statements using the ExecuteCommand function, in other words without creating an entity:
      1. using (var ObjEntity = new CRMModel.CRMEntities())  
      2. {   
      3. var UpdateResult= ObjEntity.ExecuteStoreCommand("update customer set Name='saip' where id=1");  
      4. }  

    Find the attached source code.

    In the next article, we will learn how to return multiples entities from a single Stored Procedure like we return multiple tables from a Stored Procedure and catch as a dataset in ADO.NET using fill methods.

    Note: Before running the source code please change the web.config connection string.

    Thanks for reading.

    Up Next
      Ebook Download
      View all
      Learn
      View all