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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace Select_Data_CFA___Fluent_API
- {
- public class Employee
- {
- public Employee()
- {
- }
-
- public int Id { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public int Age { get; set; }
- }
- }
Employeecontext.cs
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Web;
-
- namespace Select_Data_CFA___Fluent_API
- {
- public class EmployeeContext : DbContext
- {
-
- public EmployeeContext()
- : base("EmployeeConn")
- {
- Database.SetInitializer<EmployeeContext>(new CreateDatabaseIfNotExists<EmployeeContext>());
- }
-
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
-
- modelBuilder.Entity<Employee>().HasKey(m => m.Id).Property(m => m.Id).IsRequired();
-
-
- modelBuilder.Entity<Employee>().Property(p => p.FirstName).IsRequired().HasMaxLength(50);
-
-
- modelBuilder.Entity<Employee>().Property(p => p.LastName).IsRequired().HasMaxLength(50);
-
-
- modelBuilder.Entity<Employee>().Property(p => p.Age).IsRequired();
- }
-
- public DbSet<Employee> Employees { get; set; }
- }
- }
Web.config
- <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_CFA___Fluent_API.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 CFA - Fluent API" 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>
Webform1.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- namespace Select_Data_CFA___Fluent_API
- {
- 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();
- }
- }
- }
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!