Insert Data Using Code First Approach

Introduction

This article shows how to insert data using the Code First approach.

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. using System.ComponentModel.DataAnnotations;  
  6. using System.ComponentModel.DataAnnotations.Schema;  
  7.   
  8. namespace Insert_Data___Code_Frist_Approach  
  9. {  
  10.     public class Employee  
  11.     {  
  12.   
  13.         public Employee()  
  14.         {   
  15.         }  
  16.   
  17.         [Key]  
  18.         [DatabaseGenerated(DatabaseGeneratedOption.Identity)]  
  19.         public int Id { getset; }  
  20.         public string FirstName { getset; }  
  21.         public string LastName { getset; }  
  22.         public int Age { getset; }  
  23.     }  
  24. }  
 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 Insert_Data___Code_Frist_Approach  
  8. {  
  9.     public class EmployeeContext : DbContext  
  10.     {  
  11.         public EmployeeContext()  
  12.             : base("EmployeeConn")  
  13.         {  
  14.             Database.SetInitializer<EmployeeContext>(new CreateDatabaseIfNotExists<EmployeeContext>());            
  15.         }  
  16.   
  17.         public DbSet<Employee> Employees { getset; }  
  18.     }  
  19. }  
 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="Insert_Data___Code_Frist_Approach.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.   
  13.             <table>  
  14.                 <tr>  
  15.                     <td>  
  16.                         <asp:Label ID="Label1" runat="server" Text="Insert Data - EF Code First Approach" Font-Bold="true"></asp:Label>  
  17.                     </td>  
  18.                 </tr>  
  19.             </table>  
  20.             <br />  
  21.             <br />  
  22.             <table>  
  23.                 <tr>  
  24.                     <td>  
  25.                         <asp:Label ID="Label2" runat="server" Text="Please Enter FirstName: " ForeColor="Brown"  
  26.                             Font-Bold="true" Font-Italic="true"></asp:Label>  
  27.                     </td>  
  28.                     <td>  
  29.                         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  30.                     </td>  
  31.                 </tr>  
  32.                 <tr>  
  33.                     <td>  
  34.                         <asp:Label ID="Label3" runat="server" Text="Please Enter LastName: " ForeColor="Brown"  
  35.                             Font-Bold="true" Font-Italic="true"></asp:Label>  
  36.                     </td>  
  37.                     <td>  
  38.                         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
  39.                     </td>  
  40.                 </tr>  
  41.                 <tr>  
  42.                     <td>  
  43.                         <asp:Label ID="Label4" runat="server" Text="Please Enter Age: " ForeColor="Brown"  
  44.                             Font-Bold="true" Font-Italic="true"></asp:Label>  
  45.                     </td>  
  46.                     <td>  
  47.                         <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
  48.                     </td>  
  49.                 </tr>  
  50.                 <tr>  
  51.                     <td colspan="2">  
  52.                         <asp:Button ID="Button1" runat="server" Text="Insert Data"  
  53.                             BackColor="Orange" Font-Bold="true" OnClick="Button1_Click" />  
  54.                         <br />  
  55.                         <br />  
  56.                     </td>  
  57.                 </tr>  
  58.             </table>  
  59.             <br />  
  60.             <br />  
  61.             <table>  
  62.                 <tr>  
  63.                     <td colspan="3">  
  64.                         <asp:Label ID="Label5" runat="server" ForeColor="Red" Font-Bold="true"></asp:Label>  
  65.                     </td>  
  66.                 </tr>  
  67.             </table>  
  68.   
  69.         </div>  
  70.     </form>  
  71. </body>  
  72. </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 Insert_Data___Code_Frist_Approach  
  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.             Label5.Text = String.Empty;  
  20.             EmployeeContext empContext = new EmployeeContext();  
  21.             Employee emp = new Employee() { FirstName = TextBox1.Text, LastName = TextBox2.Text, Age = int.Parse(TextBox3.Text) };  
  22.             empContext.Employees.Add(emp);  
  23.             empContext.SaveChanges();  
  24.             Label5.Text = "Data Inserted Successfully";  
  25.   
  26.         }  
  27.     }  
  28. }  
The output of the application looks as in the following.
 

Summary

In this article, we saw how to insert data using the Code First approach. Happy coding!