Update and Delete Data Using Code First Approach Fluent API

Update Data using Code First Approach Fluent API

This article shows how to access a SQL Server database with the Entity Framework Code First Approach and later we will also look at how to do a update data operation.

Create an ASP.Net web application as in the following.

Employee.cs

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5.     
  6. namespace Update_Data_CFA___Fluent_API    
  7. {    
  8.     public class Employee    
  9.     {    
  10.         public Employee()    
  11.         {    
  12.         }    
  13.     
  14.         public int Id { getset; }    
  15.         public string FirstName { getset; }    
  16.         public string LastName { getset; }    
  17.         public int Age { getset; }    
  18.     }    
  19. }   

Employeecontext.cs

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Data.Entity;    
  4. using System.Linq;    
  5. using System.Web;    
  6.     
  7. namespace Update_Data_CFA___Fluent_API    
  8. {    
  9.     public class EmployeeContext: DbContext    
  10.     {    
  11.         public EmployeeContext()    
  12.             : base("EmployeeConn")    
  13.         {    
  14.             Database.SetInitializer<EmployeeContext>(new CreateDatabaseIfNotExists<EmployeeContext>());    
  15.         }    
  16.     
  17.         protected override void OnModelCreating(DbModelBuilder modelBuilder)    
  18.         {    
  19.             //Set primary key to Employee table    
  20.             modelBuilder.Entity<Employee>().HasKey(m => m.Id).Property(m => m.Id).IsRequired();    
  21.     
  22.             //First Name is required and Max Length is 50    
  23.             modelBuilder.Entity<Employee>().Property(p => p.FirstName).IsRequired().HasMaxLength(50);    
  24.     
  25.             //Last Name is required and Max Length is 50    
  26.             modelBuilder.Entity<Employee>().Property(p => p.LastName).IsRequired().HasMaxLength(50);    
  27.     
  28.             //Age is required     
  29.             modelBuilder.Entity<Employee>().Property(p => p.Age).IsRequired();    
  30.         }    
  31.     
  32.         public DbSet<Employee> Employees { getset; }    
  33.     }    
  34. }   

Web.config

  1. <connectionStrings>    
  2.    <add name="EmployeeConn"    
  3.    connectionString="Data Source=WIN-B4KJ8JI75VF;Initial Catalog=EmployeeDB;Integrated Security=true"    
  4.    providerName="System.Data.SqlClient"/>    
  5. </connectionStrings>    
Webform1.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Update_Data_CFA___Fluent_API.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.      <table>    
  13.                 <tr>    
  14.                     <td>    
  15.                         <asp:Label ID="Label1" runat="server" Text="Update Data CFA - Fluent API" Font-Bold="true"></asp:Label>    
  16.                     </td>    
  17.                 </tr>    
  18.             </table>    
  19.             <br />    
  20.             <br />    
  21.             <table>    
  22.                 <tr>    
  23.                     <td>    
  24.                         <asp:Label ID="Label6" runat="server" Text="Please Enter Id: " ForeColor="Brown"    
  25.                             Font-Bold="true" Font-Italic="true"></asp:Label>    
  26.                     </td>    
  27.                     <td>    
  28.                         <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>    
  29.                     </td>    
  30.                 </tr>    
  31.                 <tr>    
  32.                     <td>    
  33.                         <asp:Label ID="Label2" runat="server" Text="Please Enter FirstName: " ForeColor="Brown"    
  34.                             Font-Bold="true" Font-Italic="true"></asp:Label>    
  35.                     </td>    
  36.                     <td>    
  37.                         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>    
  38.                     </td>    
  39.                 </tr>    
  40.                 <tr>    
  41.                     <td>    
  42.                         <asp:Label ID="Label3" runat="server" Text="Please Enter LastName: " ForeColor="Brown"    
  43.                             Font-Bold="true" Font-Italic="true"></asp:Label>    
  44.                     </td>    
  45.                     <td>    
  46.                         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>    
  47.                     </td>    
  48.                 </tr>    
  49.                 <tr>    
  50.                     <td>    
  51.                         <asp:Label ID="Label4" runat="server" Text="Please Enter Age: " ForeColor="Brown"    
  52.                             Font-Bold="true" Font-Italic="true"></asp:Label>    
  53.                     </td>    
  54.                     <td>    
  55.                         <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>    
  56.                     </td>    
  57.                 </tr>    
  58.                 <tr>    
  59.                     <td colspan="2">    
  60.                         <asp:Button ID="Button1" runat="server" Text="Update Data"    
  61.                             BackColor="Orange" Font-Bold="true" OnClick="Button1_Click" />    
  62.                         <br />    
  63.                         <br />    
  64.                     </td>    
  65.                 </tr>    
  66.             </table>    
  67.             <br />    
  68.             <br />    
  69.             <table>    
  70.                 <tr>    
  71.                     <td colspan="3">    
  72.                         <asp:Label ID="Label5" runat="server" ForeColor="Red" Font-Bold="true"></asp:Label>    
  73.                     </td>    
  74.                 </tr>    
  75.             </table>    
  76.     </div>    
  77.     </form>    
  78. </body>    
  79. </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 Update_Data_CFA___Fluent_API    
  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.             EmployeeContext empContext = new EmployeeContext();    
  20.             var empId = int.Parse(TextBox4.Text);    
  21.             var emp = empContext.Employees.Where(a => a.Id.Equals(empId)).SingleOrDefault();    
  22.             emp.FirstName = TextBox1.Text;    
  23.             emp.LastName = TextBox2.Text;    
  24.             emp.Age = int.Parse(TextBox3.Text);    
  25.             empContext.SaveChanges();    
  26.         }    
  27.     }    
  28. }    

The output of the application looks as in the following.

 
 

Summary: In this operation we saw how to access a SQL Server database with the Entity Framework Code First Approach and how to do a update data operation. Happy coding.

Delete Data using Code First Approach Fluent API

In this operation we will see how to access a SQL Server database with the Entity Framework Code First Approach and later we will also look at how to do a delete data operation.

Create an ASP.Net web application as in the following.

Employee.cs

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5.     
  6. namespace Delete_Data_CFA___Fluent_API    
  7. {    
  8.     public class Employee    
  9.     {    
  10.         public Employee()    
  11.         {    
  12.         }    
  13.     
  14.         public int Id { getset; }    
  15.         public string FirstName { getset; }    
  16.         public string LastName { getset; }    
  17.         public int Age { getset; }    
  18.     }    
  19. }  

Employeecontext.cs

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Data.Entity;    
  4. using System.Linq;    
  5. using System.Web;    
  6.     
  7. namespace Delete_Data_CFA___Fluent_API    
  8. {    
  9.     public class EmployeeContext: DbContext    
  10.     {    
  11.         public EmployeeContext()    
  12.             : base("EmployeeConn")    
  13.         {    
  14.             Database.SetInitializer<EmployeeContext>(new CreateDatabaseIfNotExists<EmployeeContext>());    
  15.         }    
  16.     
  17.         protected override void OnModelCreating(DbModelBuilder modelBuilder)    
  18.         {    
  19.             //Set primary key to Employee table    
  20.             modelBuilder.Entity<Employee>().HasKey(m => m.Id).Property(m => m.Id).IsRequired();    
  21.     
  22.             //First Name is required and Max Length is 50    
  23.             modelBuilder.Entity<Employee>().Property(p => p.FirstName).IsRequired().HasMaxLength(50);    
  24.     
  25.             //Last Name is required and Max Length is 50    
  26.             modelBuilder.Entity<Employee>().Property(p => p.LastName).IsRequired().HasMaxLength(50);    
  27.     
  28.             //Age is required     
  29.             modelBuilder.Entity<Employee>().Property(p => p.Age).IsRequired();    
  30.         }    
  31.     
  32.         public DbSet<Employee> Employees { getset; }    
  33.     }    
  34. }  

Web.config

  1. <connectionStrings>    
  2.    <add name="EmployeeConn"    
  3.    connectionString="Data Source=WIN-B4KJ8JI75VF;Initial Catalog=EmployeeDB;Integrated Security=true"    
  4.    providerName="System.Data.SqlClient"/>    
  5. </connectionStrings>    

Webform1.asp

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Delete_Data_CFA___Fluent_API.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.       <table>    
  13.                 <tr>    
  14.                     <td>    
  15.                         <asp:Label ID="Label1" runat="server" Text="Delete Data CFA - Fluent API" Font-Bold="true"></asp:Label>    
  16.                     </td>    
  17.                 </tr>    
  18.             </table>    
  19.             <br />    
  20.             <br />    
  21.             <table>    
  22.                 <tr>    
  23.                     <td>    
  24.                         <asp:Label ID="Label6" runat="server" Text="Please Enter Id: " ForeColor="Brown"    
  25.                             Font-Bold="true" Font-Italic="true"></asp:Label>    
  26.                     </td>    
  27.                     <td>    
  28.                         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>    
  29.                     </td>    
  30.                 </tr>    
  31.                 <tr>    
  32.                     <td colspan="2">    
  33.                         <asp:Button ID="Button1" runat="server" Text="Delete Data"    
  34.                             BackColor="Orange" Font-Bold="true" OnClick="Button1_Click" />    
  35.                         <br />    
  36.                         <br />    
  37.                     </td>    
  38.                 </tr>    
  39.             </table>    
  40.             <br />    
  41.             <br />    
  42.             <table>    
  43.                 <tr>    
  44.                     <td colspan="3">    
  45.                         <asp:Label ID="Label5" runat="server" ForeColor="Red" Font-Bold="true"></asp:Label>    
  46.                     </td>    
  47.                 </tr>    
  48.             </table>    
  49.     </div>    
  50.     </form>    
  51. </body>    
  52. </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 Delete_Data_CFA___Fluent_API    
  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.             EmployeeContext empContext = new EmployeeContext();    
  20.             var empID = int.Parse(TextBox1.Text);    
  21.             var emp = empContext.Employees.Where(a => a.Id.Equals(empID)).SingleOrDefault();    
  22.             empContext.Employees.Remove(emp);    
  23.             empContext.SaveChanges();    
  24.         }    
  25.     
  26.     
  27.     }    
  28. }    

 

The output of the application looks as in the following.

Summary: In this operation we saw how to access a SQL Server database with the Entity Framework Code First Approach and how to do a delete data operation. Happy coding!

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