Introduction
This article shows how to select data using the Code First approach.
Create ASP.Net web application
Employee.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.ComponentModel.DataAnnotations;
- using System.ComponentModel.DataAnnotations.Schema;
- namespace Insert_Data___Code_Frist_Approach
- {
- public class Employee
- {
- public Employee()
- {
- }
- [Key]
- [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
- public int Id { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public int Age { get; set; }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Web;
- namespace Insert_Data___Code_Frist_Approach
- {
- public class EmployeeContext : DbContext
- {
- public EmployeeContext()
- : base("EmployeeConn")
- {
- Database.SetInitializer<EmployeeContext>(new CreateDatabaseIfNotExists<EmployeeContext>());
- }
- public DbSet<Employee> Employees { get; set; }
- }
- }
- <connectionStrings>
- <add name="EmployeeConn"
- connectionString="Data Source=WIN-B4KJ8JI75VF;Initial Catalog=EmployeeDB;Integrated Security=true"
- providerName="System.Data.SqlClient"/>
- </connectionStrings>
Webform1.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Select_Data___EF_Code_First_Approach.WebForm1" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td>
- <asp:Label ID="Label1" runat="server" Text="Select Data - EF Code First Approach" Font-Bold="true"></asp:Label>
- </td>
- </tr>
- </table>
- <br />
- <br />
- <table>
- <tr>
- <td colspan="2">
- <asp:Button ID="Button1" runat="server" Text="Select Data"
- BackColor="Orange" Font-Bold="true" OnClick="Button1_Click" />
- <br />
- <br />
- </td>
- </tr>
- <tr>
- <td>
- <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
- <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510"></FooterStyle>
- <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White"></HeaderStyle>
- <PagerStyle HorizontalAlign="Center" ForeColor="#8C4510"></PagerStyle>
- <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510"></RowStyle>
- <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White"></SelectedRowStyle>
- <SortedAscendingCellStyle BackColor="#FFF1D4"></SortedAscendingCellStyle>
- <SortedAscendingHeaderStyle BackColor="#B95C30"></SortedAscendingHeaderStyle>
- <SortedDescendingCellStyle BackColor="#F1E5CE"></SortedDescendingCellStyle>
- <SortedDescendingHeaderStyle BackColor="#93451F"></SortedDescendingHeaderStyle>
- </asp:GridView>
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace Select_Data___EF_Code_First_Approach
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- EmployeeContext empContext = new EmployeeContext();
- var query = from r in empContext.Employees select r;
- GridView1.DataSource = query.ToList();
- GridView1.DataBind();
- }
- }
- }

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

Sonu ChaudharyPosted May 26, 2016, 10:47 AM
Good one...
Santhakumar MunuswamyPosted May 18, 2015, 2:21 PM
Thanks for sharing