Table Splitting in ASP.Net

In Entity Splitting means splitting this entity into multiple database tables  but in Table Splitting we split one table into two tables.
Entity Splitting refers to mapping an entity to two or more tables when the tables have a common key (common column).

Why to use Table Splitting in EF? One reason would be to delay some property, in other words Lazy Loading of your objects.
We will explain how we use Table Splitting in EF with an example.
Step 1: First create a table named Student.
For this execute the following script in SQL Server.
  1. Create table Students  
  2. (  
  3.    StudentID int primary key identity,  
  4.    FirstName nvarchar(50),  
  5.    LastName nvarchar(50),  
  6.    Gender nvarchar(50),  
  7.    Email nvarchar(50),  
  8.    Mobile nvarchar(50)  
  9. )  
  10. Insert into Students values ('Munesh''Sharma''Male','[email protected]','5555555555')  
  11. Insert into Students values ('Rahul''Sharma''Male','[email protected]','333333333')  
  12. Insert into Students values ('Sara''vilium''Female','[email protected]','111111111')  
  13. Insert into Students values ('Mark''hash''Female','[email protected]','2222222222')  
  14. Insert into Students values ('ABC''EFG''Male','[email protected]','6666666666'
After executing that script you will see a table in your database with the name Student.

Now go to your application and right-click on the Solution Explorer and add an “ADO.Net Entity Data Model” and select the Database First Approach.


Then click on "Next" and select the database connection and select the database name and then click on "Next" and select your tables that you created now and click Finish.

When you click on Finish you will see a “Student” entity.



Now in this entity we have Mobile and email properties. We are not using these properties everywhere, like Firstname , Lastname and Gender properties. If we load the Student entity then all these entities will be loaded automatically, so we will now create 2 entities (Student and StudentContactDetails).

The following is the procedure to do that.
1. Right-click on the Entity Designer and click on “Add entity”. Set some values as in here:




a: Entity Name = StudentContactDetail
b: Base type = None
c: Entity Set = StudentContactDetail
d: Key Property check Box = checked
e: Property name : StudentId
f: Property type : Int32



2. Now cut Mobile and Email from the Student entity and paste it into StudentContactDetail.
Then the entity will look like:


3. Again right-click on the Entity Designer and add an “Association” and fill in the following details.


4. Now right-click on the association and click on properties. When you click on properties a window will open and from there select “Referential Constraints” and fill in the details.


















 






5.
Now right-click on the StudentContactsDetails entity and click on mapping.







 
 
6. Right-click on the Solution Explorer and add a Webform and Drag down a GridView and 2 buttons.
  1. <div style="font-family:Arial">  
  2.     <asp:Button ID="FullDataWithContactDetail" runat="server" Text="GetStudent Data with contact detail"  
  3. onclick="Button1_Click" />  
  4.     <br />  
  5.     <asp:Button ID="Button2" runat="server" Text="Get Student Data"  
  6. onclick="Button2_Click" />  
  7.     <asp:GridView ID="GridView1" runat="server"></asp:GridView>  
  8. </div> 
7. Use the following code in this Webform (in the code behind file).
  1. public partial class WebForm1: System.Web.UI.Page   
  2. {  
  3.     private DataTable GetStudentData()   
  4.     {  
  5.         StudentDBContext _studentDBContext = new StudentDBContext();  
  6.         List < Student > _students = studentDBContext.Student.ToList();  
  7.         DataTable dt = new DataTable();  
  8.         DataColumn[] columns =   
  9.         {  
  10.             new DataColumn("StudentID"),  
  11.             new DataColumn("FirstName"),  
  12.             new DataColumn("LastName"),  
  13.             new DataColumn("Gender")  
  14.         };  
  15.         dt.Columns.AddRange(columns);  
  16.         foreach(Student students in _students)   
  17.         {  
  18.             DataRow dr = dt.NewRow();  
  19.             dr["StudentsID"] = students.StudentsID;  
  20.             dr["FirstName"] = students.FirstName;  
  21.             dr["LastName"] = students.LastName;  
  22.             dr["Gender"] = students.Gender;  
  23.             dt.Rows.Add(dr);  
  24.         }  
  25.         return dt;  
  26.     } 
  1. private DataTable GetStudentDatawithContactDetails()   
  2. {  
  3.     StudentDBContext _studentDBContext = new StudentDBContext();  
  4.     List < Student > _student = _ studentDBContext.Student.Include("StudentContactDetail").ToList();  
  5.     DataTable dt = new DataTable();  
  6.     DataColumn[] columns = {  
  7.         new DataColumn("StudentID"),  
  8.         new DataColumn("FirstName"),  
  9.         new DataColumn("LastName"),  
  10.         new DataColumn("Gender"),  
  11.         new DataColumn("Email"),  
  12.         new DataColumn("Mobile")  
  13.         dt.Columns.AddRange(columns);  
  14.         foreach(Student student in _student)   
  15.         {  
  16.             DataRow dr = dt.NewRow();  
  17.             dr["StudentID"] = student.StudentID;  
  18.             dr["FirstName"] = student.FirstName;  
  19.             dr["LastName"] = student.LastName;  
  20.             dr["Gender"] = student.Gender;  
  21.             dr["Email"] = student.StudentContactDetail.Email;  
  22.             dr["Mobile"] = student.StudentContactDetail.Mobile;  
  23.             dataTable.Rows.Add(dr);  
  24.         }  
  25.         return dt;  
  26.     }  
  27.     protected void Button1_Click(object sender, EventArgs e)   
  28.     {  
  29.         GridView1.DataSource = GetStudentDatawithContactDetails();  
  30.         GridView1.DataBind();  
  31.     } 
  1. protected void Button2_Click(object sender, EventArgs e)  
  2. {  
  3.    GetStudentData();  

8. Now run your application and check that your data is with contact detail and without contact detail.

You can go to my blog_munesh.

Up Next
    Ebook Download
    View all
    Learn
    View all