Select Data Using Code First Approach

Introduction

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

Create ASP.Net web application

 

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="Select_Data___EF_Code_First_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.       <table>  
  13.                 <tr>  
  14.                     <td>  
  15.                         <asp:Label ID="Label1" runat="server" Text="Select Data - EF Code First Approach" Font-Bold="true"></asp:Label>  
  16.                     </td>  
  17.                 </tr>  
  18.             </table>  
  19.             <br />  
  20.             <br />  
  21.             <table>  
  22.                 <tr>  
  23.                     <td colspan="2">  
  24.                         <asp:Button ID="Button1" runat="server" Text="Select Data"  
  25.                             BackColor="Orange" Font-Bold="true" OnClick="Button1_Click" />  
  26.                         <br />  
  27.                         <br />  
  28.                     </td>  
  29.                 </tr>  
  30.                 <tr>  
  31.                     <td>  
  32.                         <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">  
  33.                             <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510"></FooterStyle>  
  34.   
  35.                             <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White"></HeaderStyle>  
  36.   
  37.                             <PagerStyle HorizontalAlign="Center" ForeColor="#8C4510"></PagerStyle>  
  38.   
  39.                             <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510"></RowStyle>  
  40.   
  41.                             <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White"></SelectedRowStyle>  
  42.   
  43.                             <SortedAscendingCellStyle BackColor="#FFF1D4"></SortedAscendingCellStyle>  
  44.   
  45.                             <SortedAscendingHeaderStyle BackColor="#B95C30"></SortedAscendingHeaderStyle>  
  46.   
  47.                             <SortedDescendingCellStyle BackColor="#F1E5CE"></SortedDescendingCellStyle>  
  48.   
  49.                             <SortedDescendingHeaderStyle BackColor="#93451F"></SortedDescendingHeaderStyle>  
  50.                         </asp:GridView>  
  51.   
  52.   
  53.                     </td>  
  54.                 </tr>  
  55.             </table>  
  56.              
  57.     </div>  
  58.     </form>  
  59. </body>  
  60. </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 Select_Data___EF_Code_First_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.             EmployeeContext empContext = new EmployeeContext();  
  20.             var query = from r in empContext.Employees select r;  
  21.             GridView1.DataSource = query.ToList();  
  22.             GridView1.DataBind();  
  23.         }  
  24.     }  
  25. }  
The following is the output of the application.
 
 
Summary

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