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
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Table_Splitting_Delete_Data.WebForm1" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Button ID="Button1" runat="server" Text="Delete" OnClick="Button1_Click" style="height: 26px" />
- </div>
- </form>
- </body>
- </html>
Webform1.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- namespace Table_Splitting_Delete_Data
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- protected void Button1_Click(object sender, EventArgs e)
- {
- using (var dbContext = new EmployeeDBEntities())
- {
- var master = dbContext.Set<Employee>().Include(m => m.EmployeeDetail)
- .SingleOrDefault(m => m.EmpId == 1);
- dbContext.Set<Employee>().Remove(master);
- dbContext.SaveChanges();
- }
- }
- }
- }
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!