Wednesday, November 21, 2018

পার্ট 10 - Entity splitting in entity framework (DB_First Approach)

Suggested Articles
পার্ট 7 - Using stored procedures with entity framework
পার্ট 8 - Using stored procedures with the entity framework code first approach
পার্ট 9 - Overriding stored procedure defaults with the entity framework code first approach

Entity Framework এর Database First approach এ যখন দুই বা ততোধিক Database table একটা common key শেয়ার করে, তখন Entity mapping এর সাহায্যে কিভাবে Entity splitting করা যায় সে বিষয়ে আলোচনা করবো। চলুন একটা উদাহরণ দিয়ে দেখা যাক -

আমদের কাছে ২ টা table আছে। এই দুই টেবিলই একটা common key শেয়ার করে তার নাম EmployeeID

Entity splitting in entity framework
EmployeeContactDetails Table

Entity splitting in entity framework
EmployeeContactDetails Table 

টেবিল ২ টা create করে এদের test data দিয়ে populate করার জন্য SQL Script -
Create table Employees
(
     EmployeeID int primary key identity,
     FirstName nvarchar(50),
     LastName nvarchar(50),
     Gender nvarchar(50)
)
GO

Create table EmployeeContactDetails
(
     EmployeeID int primary key,
     Email nvarchar(50),
     Mobile nvarchar(50),
     LandLine nvarchar(50),
     CONSTRAINT fk_pk_empid FOREIGN   KEY(EmployeeIDREFERENCES  Employees (EmployeeID)
)
GO

Insert into Employees values ('Mark', 'Hastings', 'Male')
Insert into Employees values ('Steve', 'Pound', 'Male')
Insert into Employees values ('Ben', 'Hoskins', 'Male')
Insert into Employees values ('Philip', 'Hastings', 'Male')
Insert into Employees values ('Mary', 'Lambeth', 'Female')

Insert into EmployeeContactDetails values
(1, 'Mark@pragimtech.com', '111111111', '111111111')
Insert into EmployeeContactDetails values
(2, 'Steve@pragimtech.com', '2222222222', '2222222222')
Insert into EmployeeContactDetails values
(3, 'Ben@pragimtech.com', '3333333333', '3333333333')
Insert into EmployeeContactDetails values
(4, 'Philip@pragimtech.com', '44444444444', '44444444444')
Insert into EmployeeContactDetails values
(5, 'Mary@pragimtech.com', '5555555555', '5555555555')

এখন যদি আমরা  Entity Framework এর database first approach এ entity generate করি তাহলে by default ২ টা entities create হবে। i.e Employee এবং EmployeeContactDetail entities -
Entity splitting in entity framework
EmployeeModel.edmx
এখন আমরা Employee নামের একটা entity চাই যেটা Employees এবং EmployeeContactDetails নামের দুইটা টেবিলকেই map করবে।

To achieve this 
1. Cut Email, Mobile and LandLine properties from the EmployeeContactDetail entity and paste them in Employee entity.
2. Delete EmployeeContactDetail entity. On "Delete Unmapped Tables and Views" window click NO.
3. Right click on the Employee entity and select "Table Mapping" option from the context menu. Map EmployeeId, Email, Mobile and LandLine properties to the respective columns of EmployeeContactDetails table.
Entity splitting in entity framework-Table Mapping
Table Mapping

At this point, we have only one Entity. Build the solution. Add a WebForm. Drag and drop the following 3 controls.
1. GridView
2. DetailsView
3. EntityDataSource

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 Employees from EntitySetName drop-down list and enable Inserts, Updates, and Deletes.

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" drop-down list
d) Select Enable Editing and Enable Deleting checkboxes

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" dropdownlist
d) Select Enable Inserting checkbox
e) Set DeafultMode=Insert. Use properties window to set this.
f) Set InsertVisible="false" for the EmployeeID 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.
protected void DetailsView1_ItemInserted
                                 (object sender, DetailsViewInsertedEventArgs e)
{
GridView1.DataBind();
}

At this point run the application. Insert, update and delete an Employee, and notice that both the tables (Employees and EmployeeContactDetails) are updated as expected.

webForm should look like following-
Entity splitting in entity framework - Output
webForm1


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



No comments:

Post a Comment