Table Splitting Delete Data

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.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <asp:Button ID="Button1" runat="server" Text="Delete" OnClick="Button1_Click" style="height: 26px" />  
  13.     </div>  
  14.     </form>  
  15. </body>  
  16. </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.   
  9. namespace Table_Splitting_Delete_Data  
  10. {  
  11.     public partial class WebForm1 : System.Web.UI.Page  
  12.     {  
  13.         protected void Page_Load(object sender, EventArgs e)  
  14.         {  
  15.   
  16.         }  
  17.   
  18.         protected void Button1_Click(object sender, EventArgs e)  
  19.         {  
  20.             using (var dbContext = new EmployeeDBEntities())  
  21.             {  
  22.                 var master = dbContext.Set<Employee>().Include(m => m.EmployeeDetail)  
  23.                         .SingleOrDefault(m => m.EmpId == 1);  
  24.                 dbContext.Set<Employee>().Remove(master);  
  25.                 dbContext.SaveChanges();  
  26.             }  
  27.         }  
  28.     }  
  29. }

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!


Similar Articles
MVC Corporation
MVC Corporation is consulting and IT services based company.