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. namespace Insert_Data___Code_Frist_Approach
  8. {
  9. public class Employee
  10. {
  11. public Employee()
  12. {
  13. }
  14. [Key]
  15. [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  16. public int Id { get; set; }
  17. public string FirstName { get; set; }
  18. public string LastName { get; set; }
  19. public int Age { get; set; }
  20. }
  21. }
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 Insert_Data___Code_Frist_Approach
  7. {
  8. public class EmployeeContext : DbContext
  9. {
  10. public EmployeeContext()
  11. : base("EmployeeConn")
  12. {
  13. Database.SetInitializer<EmployeeContext>(new CreateDatabaseIfNotExists<EmployeeContext>());
  14. }
  15. public DbSet<Employee> Employees { get; set; }
  16. }
  17. }
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. <!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="Select Data - EF Code First Approach" Font-Bold="true"></asp:Label>
  14. </td>
  15. </tr>
  16. </table>
  17. <br />
  18. <br />
  19. <table>
  20. <tr>
  21. <td colspan="2">
  22. <asp:Button ID="Button1" runat="server" Text="Select Data"
  23. BackColor="Orange" Font-Bold="true" OnClick="Button1_Click" />
  24. <br />
  25. <br />
  26. </td>
  27. </tr>
  28. <tr>
  29. <td>
  30. <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
  31. <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510"></FooterStyle>
  32. <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White"></HeaderStyle>
  33. <PagerStyle HorizontalAlign="Center" ForeColor="#8C4510"></PagerStyle>
  34. <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510"></RowStyle>
  35. <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White"></SelectedRowStyle>
  36. <SortedAscendingCellStyle BackColor="#FFF1D4"></SortedAscendingCellStyle>
  37. <SortedAscendingHeaderStyle BackColor="#B95C30"></SortedAscendingHeaderStyle>
  38. <SortedDescendingCellStyle BackColor="#F1E5CE"></SortedDescendingCellStyle>
  39. <SortedDescendingHeaderStyle BackColor="#93451F"></SortedDescendingHeaderStyle>
  40. </asp:GridView>
  41. </td>
  42. </tr>
  43. </table>
  44. </div>
  45. </form>
  46. </body>
  47. </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 Select_Data___EF_Code_First_Approach
  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 query = from r in empContext.Employees select r;
  18. GridView1.DataSource = query.ToList();
  19. GridView1.DataBind();
  20. }
  21. }
  22. }
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!