Entity Splitting CRUD Operations

Entity Splitting Select Operation

This article shows how to do entity splitting and how to do various data operations.

Here is the SQL Server table structure.

Start by creating an ASP.Net Web Application as shown in the following screenshot.

Now, cut columns from the employee details entity as shown in the following screenshot.

Now, paste them into the Employee entity.

Next, delete the Employee Details entity from the model.

Click No. Then a dialog box appears for "Delete Unmapped Tables and Views". The same can be seen in the following screenshot.

Do the the table mapping.

Webform1.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="EntitySplitting_SelectDataApp.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:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">    
  13.             <AlternatingRowStyle BackColor="PaleGoldenrod"></AlternatingRowStyle>    
  14.     
  15.             <FooterStyle BackColor="Tan"></FooterStyle>    
  16.     
  17.             <HeaderStyle BackColor="Tan" Font-Bold="True"></HeaderStyle>    
  18.     
  19.             <PagerStyle HorizontalAlign="Center" BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"></PagerStyle>    
  20.     
  21.             <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite"></SelectedRowStyle>    
  22.     
  23.             <SortedAscendingCellStyle BackColor="#FAFAE7"></SortedAscendingCellStyle>    
  24.     
  25.             <SortedAscendingHeaderStyle BackColor="#DAC09E"></SortedAscendingHeaderStyle>    
  26.     
  27.             <SortedDescendingCellStyle BackColor="#E1DB9C"></SortedDescendingCellStyle>    
  28.     
  29.             <SortedDescendingHeaderStyle BackColor="#C2A47B"></SortedDescendingHeaderStyle>    
  30.         </asp:GridView>    
  31.     </div>    
  32.     </form>    
  33. </body>    
  34. </html>   

Webform1.aspx.cs

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.UI;    
  6. using System.Web.UI.WebControls;    
  7.     
  8. namespace EntitySplitting_SelectDataApp    
  9. {    
  10.     public partial class WebForm1 : System.Web.UI.Page    
  11.     {    
  12.         EmployeeDBEntities objEmpEntities = new EmployeeDBEntities();    
  13.         protected void Page_Load(object sender, EventArgs e)    
  14.         {    
  15.             var query = from r in objEmpEntities.Employees    
  16.                         select new    
  17.                         {    
  18.                             r.FirstName,    
  19.                             r.LastName,    
  20.                             r.Phone,    
  21.                             r.Email    
  22.                         };    
  23.             GridView1.DataSource = query.ToList();    
  24.             GridView1.DataBind();    
  25.         }    
  26.     }    
  27. }    

The output of the application looks as shown in the following screenshot.

Summary: In this operation, we saw how to do entity splitting and select data operation.

Happy coding!

Entity Splitting Insert Operation

In this operation, we will learn how to do entity splitting and later we will also learn how to do an insert data operation. 

Here is the SQL Server table structure. 


Create an ASP.Net Web Application as shown in the following screenshot.

Now, cut the columns from the Employee Details entity.

Next, paste them into the Employee entity.

Delete the Employee Details entity from the model.

 

Click no, then a dialog box appears for "Delete Unmapped Tables and Views". The same can be seen in the following screenshot.

Do the table mapping.

Webform1.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Entity_Splitting_Insert_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="Insert Data" OnClick="Button1_Click" />    
  13.     </div>    
  14.     </form>    
  15. </body>    
  16. </html>    

Webform1.aspx.cs

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.UI;    
  6. using System.Web.UI.WebControls;    
  7.     
  8. namespace Entity_Splitting_Insert_Data    
  9. {    
  10.     public partial class WebForm1 : System.Web.UI.Page    
  11.     {    
  12.         protected void Page_Load(object sender, EventArgs e)    
  13.         {    
  14.     
  15.         }    
  16.     
  17.         protected void Button1_Click(object sender, EventArgs e)    
  18.         {    
  19.             using (var context = new EmployeeDBEntities())    
  20.             {    
  21.                 var employee = new Employee    
  22.                 {    
  23.                     FirstName = "Steve",    
  24.                     LastName = "Robin",    
  25.                     Email = "[email protected]",    
  26.                     Phone = "123443223"    
  27.                 };    
  28.     
  29.                 context.Employees.Add(employee);    
  30.                 context.SaveChanges();    
  31.             }    
  32.         }    
  33.     }    
  34. }    

The output of the application is shown in the following screenshot.

Summary: In this operation, we saw how to do entity splitting and an insert data operation. Happy coding!

Entity Splitting Delete Operation

In this operation, we will see how to do entity splitting and a delete data operation.

Here is the SQL Server table structure.

Create an ASP.Net Web Application as shown in the following screenshot.

Cut columns from the Employee details entity.

Paste them into the Employee entity.

Now, delete the Employee Details entity from the model.

 

Click no, then a dialog box appears for "Delete Unmapped Tables and Views". The same can be seen in the following screenshot.

Do the table mapping.

Webform1.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Entity_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" />    
  13.     </div>    
  14.     </form>    
  15. </body>    
  16. </html>   

Webform1.aspx.cs

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.UI;    
  6. using System.Web.UI.WebControls;    
  7.     
  8. namespace Entity_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.         }    
  16.     
  17.         protected void Button1_Click(object sender, EventArgs e)    
  18.         {    
  19.             EmployeeDBEntities empContext = new EmployeeDBEntities();    
  20.             var empID = 1;    
  21.             var emp = empContext.Employees.Where(a => a.EmpId.Equals(empID)).SingleOrDefault();    
  22.             empContext.Employees.Remove(emp);    
  23.             empContext.SaveChanges();    
  24.         }    
  25.     }    
  26. }    

The output of the application looks as in the following.

Summary: In this operation, we saw how to do the entity splitting and a delete data operation.

Happy coding!

Entity Splitting Update Operation

In this operation, we will see how to do the entity splitting and a update data operation.

Here is the SQL Server table structure.

Create an ASP.Net Web Application as shown in the following screenshot.

Now, cut columns from the Employee Details entity.

Paste them into the Employee entity.

Delete the Employee Details entity from the model.

 

Click no, then a dialog box appears for "Delete Unmapped Tables and Views". The same can be seen in the following screenshot.

Do the table mapping.

Webform1.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Entity_Splitting_UpdateData.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="Update" OnClick="Button1_Click" />    
  13.     </div>    
  14.     </form>    
  15. </body>    
  16. </html>    

Webform1.aspx.cs

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.UI;    
  6. using System.Web.UI.WebControls;    
  7.     
  8. namespace Entity_Splitting_UpdateData    
  9. {    
  10.     public partial class WebForm1 : System.Web.UI.Page    
  11.     {    
  12.         protected void Page_Load(object sender, EventArgs e)    
  13.         {    
  14.     
  15.         }    
  16.     
  17.         protected void Button1_Click(object sender, EventArgs e)    
  18.         {    
  19.             EmployeeDBEntities empContext = new EmployeeDBEntities();    
  20.             int empId = 1;    
  21.             var emp = empContext.Employees.Where(a => a.EmpId.Equals(empId)).SingleOrDefault();    
  22.             emp.FirstName = "Jonny";    
  23.             emp.Phone = "123423";    
  24.             empContext.SaveChanges();    
  25.         }    
  26.     }    
  27. }    

The output of the application can be seen in the following screenshot.

Summary: In this operation, we saw how to do the entity splitting and a update data operation.

Happy coding!

Up Next
    Ebook Download
    View all
    Learn
    View all
    MVC Corporation is consulting and IT services based company.