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. namespace Update_Data_CFA___Fluent_API
  6. {
  7. public class Employee
  8. {
  9. public Employee()
  10. {
  11. }
  12. public int Id { get; set; }
  13. public string FirstName { get; set; }
  14. public string LastName { get; set; }
  15. public int Age { get; set; }
  16. }
  17. }

Employeecontext.cs

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

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. <!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. <table>
  11. <tr>
  12. <td>
  13. <asp:Label ID="Label1" runat="server" Text="Update Data CFA - Fluent API" Font-Bold="true"></asp:Label>
  14. </td>
  15. </tr>
  16. </table>
  17. <br />
  18. <br />
  19. <table>
  20. <tr>
  21. <td>
  22. <asp:Label ID="Label6" runat="server" Text="Please Enter Id: " ForeColor="Brown"
  23. Font-Bold="true" Font-Italic="true"></asp:Label>
  24. </td>
  25. <td>
  26. <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
  27. </td>
  28. </tr>
  29. <tr>
  30. <td>
  31. <asp:Label ID="Label2" runat="server" Text="Please Enter FirstName: " ForeColor="Brown"
  32. Font-Bold="true" Font-Italic="true"></asp:Label>
  33. </td>
  34. <td>
  35. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  36. </td>
  37. </tr>
  38. <tr>
  39. <td>
  40. <asp:Label ID="Label3" runat="server" Text="Please Enter LastName: " ForeColor="Brown"
  41. Font-Bold="true" Font-Italic="true"></asp:Label>
  42. </td>
  43. <td>
  44. <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  45. </td>
  46. </tr>
  47. <tr>
  48. <td>
  49. <asp:Label ID="Label4" runat="server" Text="Please Enter Age: " ForeColor="Brown"
  50. Font-Bold="true" Font-Italic="true"></asp:Label>
  51. </td>
  52. <td>
  53. <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
  54. </td>
  55. </tr>
  56. <tr>
  57. <td colspan="2">
  58. <asp:Button ID="Button1" runat="server" Text="Update Data"
  59. BackColor="Orange" Font-Bold="true" OnClick="Button1_Click" />
  60. <br />
  61. <br />
  62. </td>
  63. </tr>
  64. </table>
  65. <br />
  66. <br />
  67. <table>
  68. <tr>
  69. <td colspan="3">
  70. <asp:Label ID="Label5" runat="server" ForeColor="Red" Font-Bold="true"></asp:Label>
  71. </td>
  72. </tr>
  73. </table>
  74. </div>
  75. </form>
  76. </body>
  77. </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. namespace Update_Data_CFA___Fluent_API
  8. {
  9. public partial class WebForm1 : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. }
  14. protected void Button1_Click(object sender, EventArgs e)
  15. {
  16. EmployeeContext empContext = new EmployeeContext();
  17. var empId = int.Parse(TextBox4.Text);
  18. var emp = empContext.Employees.Where(a => a.Id.Equals(empId)).SingleOrDefault();
  19. emp.FirstName = TextBox1.Text;
  20. emp.LastName = TextBox2.Text;
  21. emp.Age = int.Parse(TextBox3.Text);
  22. empContext.SaveChanges();
  23. }
  24. }
  25. }

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. namespace Delete_Data_CFA___Fluent_API
  6. {
  7. public class Employee
  8. {
  9. public Employee()
  10. {
  11. }
  12. public int Id { get; set; }
  13. public string FirstName { get; set; }
  14. public string LastName { get; set; }
  15. public int Age { get; set; }
  16. }
  17. }

Employeecontext.cs

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

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. <!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. <table>
  11. <tr>
  12. <td>
  13. <asp:Label ID="Label1" runat="server" Text="Delete Data CFA - Fluent API" Font-Bold="true"></asp:Label>
  14. </td>
  15. </tr>
  16. </table>
  17. <br />
  18. <br />
  19. <table>
  20. <tr>
  21. <td>
  22. <asp:Label ID="Label6" runat="server" Text="Please Enter Id: " ForeColor="Brown"
  23. Font-Bold="true" Font-Italic="true"></asp:Label>
  24. </td>
  25. <td>
  26. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  27. </td>
  28. </tr>
  29. <tr>
  30. <td colspan="2">
  31. <asp:Button ID="Button1" runat="server" Text="Delete Data"
  32. BackColor="Orange" Font-Bold="true" OnClick="Button1_Click" />
  33. <br />
  34. <br />
  35. </td>
  36. </tr>
  37. </table>
  38. <br />
  39. <br />
  40. <table>
  41. <tr>
  42. <td colspan="3">
  43. <asp:Label ID="Label5" runat="server" ForeColor="Red" Font-Bold="true"></asp:Label>
  44. </td>
  45. </tr>
  46. </table>
  47. </div>
  48. </form>
  49. </body>
  50. </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. namespace Delete_Data_CFA___Fluent_API
  8. {
  9. public partial class WebForm1 : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. }
  14. protected void Button1_Click(object sender, EventArgs e)
  15. {
  16. EmployeeContext empContext = new EmployeeContext();
  17. var empID = int.Parse(TextBox1.Text);
  18. var emp = empContext.Employees.Where(a => a.Id.Equals(empID)).SingleOrDefault();
  19. empContext.Employees.Remove(emp);
  20. empContext.SaveChanges();
  21. }
  22. }
  23. }

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!