Table Splitting in Entity Framework

In entity, Splitting Means Split this entity into multiple database tables but in Table splitting, we split one table into two tables.
 
Entity Splitting refers to mapping an entity into two or more tables when the tables have a common key (common column).
 
Why we Use Table Splitting in EF: One reason for this question is that when we want to delay some property it means Lazy loading to load your objects.
 
How we use Table splitting in EF, we will understand with an example.
 
Step 1: First of all create a table with the name Student.
 
For this execute the below script in the 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''m@g.com''5555555555')  
  11. Insert into Students values('Rahul''Sharma''Male''R@g.com''333333333')  
  12. Insert into Students values('Sara''vilium''Female''S@g.com''111111111')  
  13. Insert into Students values('Mark''hash''Female''M1@g.com''2222222222')  
  14. Insert into Students values('ABC''EFG''Male''A@g.com''6666666666')  
After executing this script you will see a table in your database With the name Student.
 
Now go to your application and Right-click on solution explorer and add an “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 is created now, And click FINISH.
 
When you click on FINISH you will see one “Student” Entities
 
 
Now in this entity, we have Mobile and email properties, these properties we are not using everywhere like Firstname, Lastname, Gender properties. If we will load the Student entity these all entities will load automatically, So now we will create 2 entities (Student ) & (StudentContactDetails).
 
Getting this there are some steps:
  1. Right click on the Entity designer and click on “Add entity” .And Set some values 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 Student entity and pest it into StudentContactDetail.
     
    Then entity will look like that,
     
     
  3. Again Right click on entity designer and Add an “Association” And fill in the Some details.
     
     
  4. Now Right-click on Association And click on properties, when you will click on the property a window will open from there select “Referential Constraint” and fill in Details.
     
     
     
  5. Now right click on StudentContactsDetails entity and click on mapping.
     
    StudentContactsDetails
     
  6. Right click on solution explorer and add a Webform and Dragdown a gridview and 2 buttons.
    1. <div style="font-family:Arial">  
    2.     <asp:Button ID="FullDataWithContactDetail" runat="server" Text="GetStudent Data with contact detail" onclick="Button1_Click" />  
    3.     <br />  
    4.     <asp:Button ID="Button2" runat="server" Text="Get Student Data" onclick="Button2_Click" />  
    5.     <asp:GridView ID="GridView1" runat="server">  
    6.     </asp:GridView>  
    7. </div>  
  7. Write below code on this Webform(In 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.             new DataColumn("StudentID"),  
    10.             new DataColumn("FirstName"),  
    11.             new DataColumn("LastName"),  
    12.             new DataColumn("Gender")  
    13.         };  
    14.         dt.Columns.AddRange(columns);  
    15.         foreach(Student students in _students)  
    16.         {  
    17.             DataRow dr = dt.NewRow();  
    18.             dr["StudentsID"] = students.StudentsID;  
    19.             dr["FirstName"] = students.FirstName;  
    20.             dr["LastName"] = students.LastName;  
    21.             dr["Gender"] = students.Gender;  
    22.             dt.Rows.Add(dr);  
    23.         }  
    24.         return dt;  
    25.     }  
    26.     private DataTable GetStudentDatawithContactDetails()  
    27.     {  
    28.         StudentDBContext _studentDBContext = new StudentDBContext();  
    29.         List < Student > _student = _ studentDBContext.Student  
    30.             .Include("StudentContactDetail").ToList();  
    31.         DataTable dt = new DataTable();  
    32.         DataColumn[] columns = {  
    33.             new DataColumn("StudentID"),  
    34.             new DataColumn("FirstName"),  
    35.             new DataColumn("LastName"),  
    36.             new DataColumn("Gender"),  
    37.             new DataColumn("Email"),  
    38.             new DataColumn("Mobile")  
    39.             dt.Columns.AddRange(columns);  
    40.             foreach(Student student in _student)  
    41.             {  
    42.                 DataRow dr = dt.NewRow();  
    43.                 dr["StudentID"] = student.StudentID;  
    44.                 dr["FirstName"] = student.FirstName;  
    45.                 dr["LastName"] = student.LastName;  
    46.                 dr["Gender"] = student.Gender;  
    47.                 dr["Email"] = student.StudentContactDetail.Email;  
    48.                 dr["Mobile"] = student.StudentContactDetail.Mobile;  
    49.                 dataTable.Rows.Add(dr);  
    50.             }  
    51.             return dt;  
    52.         }  
    53.         protected void Button1_Click(object sender, EventArgs e)  
    54.         {  
    55.             GridView1.DataSource = GetStudentDatawithContactDetails();  
    56.             GridView1.DataBind();  
    57.         }  
    58.         protected void Button2_Click(object sender, EventArgs e)  
    59.         {  
    60.             GetStudentData();  
    61.         }  
    62.    }  
    63. }  
  8. Now run your application and check your data with contact detail and without contact detail.