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 টা ব্যাবহার করবো -
Employees table |
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)
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
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.
DBContext Name |
Database Object Settings |
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
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 entity data source |
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 |
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 |
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