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.
- How to execute an 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");
- }
- 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.
- using (var ObjEntity = new CRMModel.CRMEntities())
- {
- var Cuetomer1 = ObjEntity.ExecuteStoreQuery<Customer>("USP_GetCustomerByID 1");
- }
- 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");
- }
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.