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

CREATE PROCEDURE [dbo].[USP_GetCustomerByID]
(
    @CustomerID int
)
AS
BEGIN
    SELECT * FROM Customer WHERE id = @CustomerID
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 a raw SQL query
    The following is another way to execute a SQL query without calling entity functions
    using (var ObjEntity = new CRMModel.CRMEntities())
    {
        var GetAllCustomer = ObjEntity.ExecuteStoreQuery<Customer>("select * from customer");
    }
    
  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 Management Studio, so you will get an appropriate procedure output.
    using (var ObjEntity = new CRMModel.CRMEntities())
    {
        var Customer1 = ObjEntity.ExecuteStoreQuery<Customer>("USP_GetCustomerByID 1");
    }
    
  3. The following executes Insert/Update/Delete Statements using the ExecuteCommand function, in other words without creating an entity
    using (var ObjEntity = new CRMModel.CRMEntities())
    {
        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 multiple 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.

Go to part 4


Similar Articles