Saturday, November 17, 2018

পার্ট 7 - Using stored procedures with entity framework

Suggested Articles
Part 4 - Customizing table & column names
Part 5 - How to handle model changes in entity framework
Part 6 - How to seed the database with test data using entity framework

নিচের explanations ভালভাবে বুঝতে এই video টি একবার দেখে আসুন। ধন্যবাদ।।


এই আর্টিকেল আমরা Entity framework এ Insert, Update এবং Delete operations গুলো perform করার জন্য কিভাবে আমরা আমাদের own custom stored procedures use করতে পারি সেই বিষয়ে discuss করবো। আমরা demo হিসেবে নিচের Employees table টা ব্যাবহার করবো -
Using stored procedures with entity frameowrk bangla
 Employees table
N.B: Practical ধাপগুলো আমি আপনাদের বোঝার সুবিধার জন্য English এ explain করবো। এই step গুলো VS2017(Visual Studio 2017) এ execute করা হয়েছে।



Step 1: Use the following SQL Script to create and populate the Employees table.
Create table Employees
(
     ID int primary key identity,
     Name nvarchar(50),
     Gender nvarchar(50),
     Salary int
)

Insert into Employees values ('Mark', 'Male', 60000)
Insert into Employees values ('Steve', 'Male', 45000)
Insert into Employees values ('Ben', 'Male', 70000)
Insert into Employees values ('Philip', 'Male', 45000)
Insert into Employees values ('Mary', 'Female', 30000)
Insert into Employees values ('Valarie', 'Female', 35000)
Insert into Employees values ('John', 'Male', 80000)


Step 2: Create Insert, Update and Delete stored procedures
Create procedure InsertEmployee
@Name nvarchar(50),
@Gender nvarchar(50),
@Salary int
as
Begin
     Insert into Employees values (@Name, @Gender, @Salary)   
End
Go

Create procedure UpdateEmployee
@ID int,
@Name nvarchar(50),
@Gender nvarchar(50),
@Salary int
as
Begin
     Update Employees Set Name = @Name, Gender = @Gender,
     Salary = @Salary
     where ID = @ID
End
Go

Create procedure DeleteEmployee
@ID int
as
Begin
     Delete from Employees where ID = @ID
End
Go

Step 3: Create a new empty asp.net web application with Entity Framework
(follow part 1)

Step 4: Add a new ADO.NET Entity Data Model.
a) On Choose Model Contents screen select "EF Designer from database" option and click Next
Using insert update delete stored procedures with entity frameowrk
Model contents selection

b) On "Choose Your Data Connections" screen give a meaningful name for the connection string that will be stored in the web.config file. I have named it EmployeeDBContext. Click Next.
executing stored procedures in entity framework
DBContext Name
c) On "Choose Your Database Objects" screen, select Employees Table and the 3 stored procedures (InsertEmployee, UpdateEmployee, DeleteEmployee). Provide a meaningful name for the Model namespace. I have named it EmployeeModel. Click Finish.
executing insert update delete stored procedures with entity frameowrk
Database Object Settings
At this point on the ADO.NET Entity Model designer surface, we should be able to see the Employee entity but not the stored procedures.

To view the stored procedures,
1. Right click on entity model designer surface and select "Model Browser" from the context menu.

2. Expand Stored Procedures folder
model browser in entity framework
Expand Stored Procedures

Step 5: Add a web form to the project. Drag and drop the following 3 controls and build the solution.
1. GridView
2. DetailsView
3. EntityDataSource

Step 6: Configure EntityDataSource control

a) Right click on EntityDataSource control and select "Show Smart Tag" option

b) Click on Configure Data Source link

c) Select EmployeeDBContext from the Named Connection drop-down list and click Next

d) Select the options on "Configure Data Selection" screen as shown in the image below and click Finish
configure entitydatasource
configure entity data source
Step 7: Configure GridView control

a). Right click on GridView control and select "Show Smart Tag" option

b) Click on "Auto Format" link and select "Colourful" scheme

c) Select "EntityDataSource1" from "Choose Data Source" dropdownlist

d) Select Enable Editing and Enable Deleting checkboxes
GridView Configuration
GridView Configuration

Step 8:
Configure DetailsView control
a) Right click on DetailsView control and select "Show Smart Tag" option
b) Click on "Auto Format" link and select "Colourful" scheme
c) Select "EntityDataSource1" from "Choose Data Source" drop-down list
d) Select Enable Inserting checkbox
e) Set DeafultMode=Insert. Use properties window to set this.
f) Set InsertVisible="false" for the ID BoundField. You can do this directly in the HTML Source.
g) Generate ItemInserted event handler method for DetailsView control. Copy and paste the following code.

DetailsViewInsertedEventArgs e)
{
    GridView1.DataBind();
}

At this point if you run the application, and if you insert, update and delete employees, by default entity framework will use the SQL it auto-generates and not our custom stored procedures.

To tell the entity framework to use the stored procedures, we have to map them to the Employee entity.

Here are the steps.
1. Right click on "Employee" entity on "EmployeeModel.edmx" and select "Stored Procedure Mapping" option from the context menu.

2. In the "Mapping Details" windows specify the Insert, Update and Delete stored procedures that you want to use with "Employee" entity
stored procedure mapping in entity framework
stored procedure mapping in entity framework

At this point,
1. Run SQL Prrofiler
2. Run the application
3. Insert, Update and Delete Employee, and notice that the respective stored procedures are being called now.

No comments:

Post a Comment