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.

Create table Students  
(  
    StudentID int primary key identity,  
    FirstName nvarchar(50),  
    LastName nvarchar(50),  
    Gender nvarchar(50),  
    Email nvarchar(50),  
    Mobile nvarchar(50)  
)  
Insert into Students values('Munesh', 'Sharma', 'Male', '[email protected]', '5555555555')  
Insert into Students values('Rahul', 'Sharma', 'Male', '[email protected]', '333333333')  
Insert into Students values('Sara', 'vilium', 'Female', '[email protected]', '111111111')  
Insert into Students values('Mark', 'hash', 'Female', '[email protected]', '2222222222')  
Insert into Students values('ABC', 'EFG', 'Male', '[email protected]', '6666666666')  

After executing this script you will see a table in your database With the name Student.

Now go to your application Right-click on Solution Explorer add an “ADO.Net Entity Data Modal” and select database first approach.

Choose model content

After that click on next select database connection select database name and then click on next and select your tables which are 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

Right-click on the Entity designer and click on “Add entity”.And Set some values here:

Entity Name: StudentContactDetail
Base type: None
Entity Set: StudentContactDetail
Key Property check Box: Checked
Property Name: StudentId
Property Type: Int32

Now Cut Mobile And Email From the Student entity and paste it into StudentContactDetail.

Then entity will look like this,

Again Right click on entity designer Add an “Association” And fill in Some details.

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.

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

StudentContactsDetails

Right-click on Solution Explorer add a Webform and down a gridview and 2 buttons.

<div style="font-family:Arial">
    <asp:Button ID="FullDataWithContactDetail" runat="server" Text="GetStudent Data with contact detail" onclick="Button1_Click" />
    <br />
    <asp:Button ID="Button2" runat="server" Text="Get Student Data" onclick="Button2_Click" />
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
</div>

Write the below code on this Webform(In code Behind the file).

public partial class WebForm1 : System.Web.UI.Page
{
    private DataTable GetStudentData()
    {
        StudentDBContext _studentDBContext = new StudentDBContext();
        List<Student> _students = _studentDBContext.Student.ToList();
        DataTable dt = new DataTable();
        DataColumn[] columns =
        {
            new DataColumn("StudentID"),
            new DataColumn("FirstName"),
            new DataColumn("LastName"),
            new DataColumn("Gender")
        };
        dt.Columns.AddRange(columns);
        foreach (Student student in _students)
        {
            DataRow dr = dt.NewRow();
            dr["StudentsID"] = student.StudentsID;
            dr["FirstName"] = student.FirstName;
            dr["LastName"] = student.LastName;
            dr["Gender"] = student.Gender;
            dt.Rows.Add(dr);
        }
        return dt;
    }
    
    private DataTable GetStudentDatawithContactDetails()
    {
        StudentDBContext _studentDBContext = new StudentDBContext();
        List<Student> _student = _studentDBContext.Student
            .Include("StudentContactDetail").ToList();
        DataTable dt = new DataTable();
        DataColumn[] columns =
        {
            new DataColumn("StudentID"),
            new DataColumn("FirstName"),
            new DataColumn("LastName"),
            new DataColumn("Gender"),
            new DataColumn("Email"),
            new DataColumn("Mobile")
        };
        dt.Columns.AddRange(columns);
        foreach (Student student in _student)
        {
            DataRow dr = dt.NewRow();
            dr["StudentID"] = student.StudentID;
            dr["FirstName"] = student.FirstName;
            dr["LastName"] = student.LastName;
            dr["Gender"] = student.Gender;
            dr["Email"] = student.StudentContactDetail.Email;
            dr["Mobile"] = student.StudentContactDetail.Mobile;
            dataTable.Rows.Add(dr);
        }
        return dt;
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = GetStudentDatawithContactDetails();
        GridView1.DataBind();
    }
    
    protected void Button2_Click(object sender, EventArgs e)
    {
        GetStudentData();
    }
}

Now run your application and check your data with contact details and without contact details.