Introduction

This article shows how to do table splitting and later we will also look at how to do a delete data operation.

SQL Server table structure


Create an ASP.Net Web Application as in the following:


Set up Entity Framework as in the following:


Cut columns from the Employee entity as in the following:


Paste them into the EmployeeDetails entity as in the following:


Add an association between Employee and EmployeeDetails as in the following:


Referential Constraints


Webform1.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Table_Splitting_Delete_Data.WebForm1" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <asp:Button ID="Button1" runat="server" Text="Delete" OnClick="Button1_Click" style="height: 26px" />
  11. </div>
  12. </form>
  13. </body>
  14. </html>

Webform1.aspx.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. namespace Table_Splitting_Delete_Data
  9. {
  10. public partial class WebForm1 : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. }
  15. protected void Button1_Click(object sender, EventArgs e)
  16. {
  17. using (var dbContext = new EmployeeDBEntities())
  18. {
  19. var master = dbContext.Set<Employee>().Include(m => m.EmployeeDetail)
  20. .SingleOrDefault(m => m.EmpId == 1);
  21. dbContext.Set<Employee>().Remove(master);
  22. dbContext.SaveChanges();
  23. }
  24. }
  25. }
  26. }

The following is the output of the application:


Before delete:

After deletion:

Summary

In this article we saw how to do table splitting and delete data operations. Happy coding!