Entity Splitting Means Split this entity into multiple database table when they have same columnname(common key).
We will understand this with an example
Step(1): Create Two table
First of all we will create two table those have same
column name with values.
In my case there are 2 table “Students” and “StudentsDetail”
Run below script in your SQL SERVER this will create two tables
In my case there are 2 table “Students” and “StudentsDetail”
Run below script in your SQL SERVER this will create two tables
Create table Students
(
StudentID int primary key identity,
FirstName nvarchar(50),
LastName nvarchar(50),
Gender nvarchar(50)
)
GO
Create table StudentContactDetails
(
StudentID int primary key,
Email nvarchar(50),
Mobile nvarchar(50)
)
GO
Insert into Students values ('Munesh',
'Sharma', 'Male')
Insert into Students values ('Rahul',
'Sharma', 'Male')
Insert into Students values ('Sara',
'vilium', 'Female')
Insert into Students values ('Mark',
'hash', 'Female')
Insert into Students values ('ABC',
'EFG', 'Male')
Insert into StudentContactDetails values
(1, 'Munesh@abc.com', '111111111')
Insert into StudentContactDetails values
(2, 'Rahul@abc.com', '2222222222')
Insert into StudentContactDetails values
(3, 'Sara@abc.com', '3333333333')
Insert into StudentContactDetails values
(4, 'Mark@abc.com', '44444444444')
Insert into EmployeeContactDetails values
(5, 'ABC@abc.com', '5555555555')
|
Step(2): Add tables to entity
Now go to your application and Right click on solution
explorer and add a “ADO.Net Entity Data Modal” and select database first approach.
After that click on next and select database connection
and select database name and then click on next and select your tables which
you created now, And click FINISH
When you click on FINISH you will see “Student” and
“StudentContactDetail” Entities
Step(3): Mapping entity
There is One to one mapping b/w entity , here we have
two entity . Now we want to single “StudentInfo” To map both “Student” and
“StudentContactDeatil” Entities.
To Achieve this there are some steps
1. Cut Email,
Mobile properties from StudentContactDetail entity
and paste in Student entity
2. Delete StudentContactDetail entity.
When you will click on Delete you will see a screen "Delete Unmapped Tables and
Views" there you click NO.
3. After that your Entity will be like that
4. Right click on Student entity and select "Table
Mapping" option from the context menu. Map StudentId,
Email, Mobile properties to the
respective columns of StudentContactDetails table.
Step(4): With an example
we have only one Entity, now Build the solution. Add a WebForm by
right click on solution explorer and add these following 3 controls from
toolBox.
GridView, DetailsView, EntityDataSource
GridView, DetailsView, EntityDataSource
a). Go to the "Show Smart Tag" option of EntityDataSource control. And click on Configure Data Source link
b) Select your connection name (in my case it is “Entitysplitting”) from the Named Connection dropdownlist and click Next
c) Select Student from EntitySetName dropdownlist and enable Inserts, Updates and Deletes Checkboxes.
Set GridView control properties
a). Go to the "Show Smart Tag" option of gridview.
b) Select "EntityDataSource1" from "Choose Data Source" dropdownlist
c) Select Enable Editing and Enable Deleting checkboxes
Set DetailsView control properties
a) go to the "Show Smart Tag" option of DetailsView .
b) Select "EntityDataSource1" from "Choose Data Source" dropdownlist
c) check (select) Enable Inserting checkbox
d) Set DeafultMode = Insert. Use properties window to set this.
e) Set InsertVisible="false" for the StudentID BoundField. This can be done through HTML Source code.
f) Generate ItemInserted event handler method for DetailsView control , And write this code
protected void DetailsViewEntity_ItemInserted(object sender,
DetailsViewInsertedEventArgse)
{
GridView1.DataBind();
}
|
now run the application. And perform these operation Insert, update , delete and notice that both the tables (Students and Students ContactDetails) are updated.