Articles → ADO.NET → Call Stored Procedure Using Entity Framework
Call Stored Procedure Using Entity Framework
- You should know how to create a table in database
- You must be aware of entity framework and how to add tables in entity framework.
- You must be aware of stored procedure in SQL server.
Click to Enlarge
CREATE TABLE [dbo].[Attribute](
[AttributeId] [int] IDENTITY(1,1) NOT NULL,
[AttributeName] [varchar](50) NULL
)
Click to Enlarge
Click to Enlarge
-- Procedure to insert value in attribute table
Create Proc InsertAttribute
@Attribute VARCHAR(50)
As
Insert into Attribute values(@Attribute)
Go
-- Procedure to get attribute based on attribute
CREATE PROC GetAttribute
@AttributeId int
As
Select * from Attribute where AttributeId = @AttributeId
Go
-- Procedure to check if the attribute exists
Create PROC IsAttributeExists
@AttributeId int
As
IF EXISTS(Select * from Attribute where AttributeId = @AttributeId)
SELECT 'EXIST'
ELSE
SELECT 'NOTEXIST'
Click to Enlarge
Click to Enlarge
Click to Enlarge
- In the first stored procedure i.e. InsertAttribute we are inserting value in table and we are not returning any value so return type in void.
- Second stored procedure GetAttribute returns an entity called ‘Attribute’.
- Third procedure checks if the attribute exists or not. The return type is string.
Click to Enlarge
Click to Enlarge
using(ABCEntities context = new ABCEntities()) {
// Proc 1
context.InsertAttribute("Name");
//Proc 2
string status = context.IsAttributeExists(1).First();
Console.WriteLine(string.Format("status = {0}", status));
// Proc 3
GetAttribute_Result attr = context.GetAttribute(1).First();
Console.WriteLine(string.Format("Attributeid= {0}{1}AttributeName= {2}", attr.AttributeId, Environment.NewLine, attr.AttributeName));
Console.ReadLine();
}
Output
Click to Enlarge