Select Data using Code First Approach Fluent API

Introduction

In this blog we will see how to access sql server database with entity framework code first approach and later we will also look at how to select data operation.

Step 1: Create ASP.NET Web Application

 

Employee.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace Select_Data_CFA___Fluent_API  
  7. {  
  8.     public class Employee  
  9.     {  
  10.         public Employee()  
  11.         {  
  12.         }  
  13.   
  14.         public int Id { getset; }  
  15.         public string FirstName { getset; }  
  16.         public string LastName { getset; }  
  17.         public int Age { getset; }  
  18.     }  
  19. }  

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 Select_Data_CFA___Fluent_API  
  8. {  
  9.     public class EmployeeContext : DbContext  
  10.     {  
  11.   
  12.         public EmployeeContext()  
  13.             : base("EmployeeConn")  
  14.         {  
  15.             Database.SetInitializer<EmployeeContext>(new CreateDatabaseIfNotExists<EmployeeContext>());  
  16.         }  
  17.   
  18.         protected override void OnModelCreating(DbModelBuilder modelBuilder)  
  19.         {  
  20.             //Set primary key to Employee table  
  21.             modelBuilder.Entity<Employee>().HasKey(m => m.Id).Property(m => m.Id).IsRequired();  
  22.   
  23.             //First Name is required and Max Length is 50  
  24.             modelBuilder.Entity<Employee>().Property(p => p.FirstName).IsRequired().HasMaxLength(50);  
  25.   
  26.             //Last Name is required and Max Length is 50  
  27.             modelBuilder.Entity<Employee>().Property(p => p.LastName).IsRequired().HasMaxLength(50);  
  28.   
  29.             //Age is required   
  30.             modelBuilder.Entity<Employee>().Property(p => p.Age).IsRequired();  
  31.         }  
  32.   
  33.         public DbSet<Employee> Employees { getset; }  
  34.     }  
  35. }  

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_CFA___Fluent_API.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 CFA - Fluent API" 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.     </div>  
  57.     </form>  
  58. </body>  
  59. </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_CFA___Fluent_API  
  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. }  

Output of the application looks like this

Summary

In this blog we have seen how we can access sql server database with entity framework code first approach and how to perform select data operation. Happy coding!

Ebook Download
View all
Learn
View all
MVC Corporation is consulting and IT services based company.