Introduction

This article shows how to access a SQL Server database with the Entity Framework Code First approach and later how to create an index.

Create ASP.Net web application

Employee.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.ComponentModel.DataAnnotations.Schema;
  5. using System.Linq;
  6. using System.Web;
  7. namespace Index_Attribute___EF
  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. [MaxLength(50)]
  19. [Index("IX_LastName_Employee", IsClustered=false)]
  20. public string LastName { get; set; }
  21. }
  22. }
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 Index_Attribute___EF
  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. }

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="Index_Attribute___EF.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="Index Attribute" Font-Bold="true"></asp:Label>
  14. </td>
  15. </tr>
  16. </table>
  17. <br />
  18. <br />
  19. <table>
  20. <tr>
  21. <td>
  22. <asp:Label ID="Label2" runat="server" Text="Please Enter FirstName: " ForeColor="Brown"
  23. Font-Bold="true" Font-Italic="true"></asp:Label>
  24. </td>
  25. <td>
  26. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  27. </td>
  28. </tr>
  29. <tr>
  30. <td>
  31. <asp:Label ID="Label3" runat="server" Text="Please Enter LastName: " ForeColor="Brown"
  32. Font-Bold="true" Font-Italic="true"></asp:Label>
  33. </td>
  34. <td>
  35. <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  36. </td>
  37. </tr>
  38. <tr>
  39. <td colspan="2">
  40. <asp:Button ID="Button1" runat="server" Text="Insert Data"
  41. BackColor="Orange" Font-Bold="true" OnClick="Button1_Click" />
  42. <br />
  43. <br />
  44. </td>
  45. </tr>
  46. </table>
  47. </div>
  48. </form>
  49. </body>
  50. </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 Index_Attribute___EF
  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. Employee emp = new Employee() { FirstName = TextBox1.Text, LastName = TextBox2.Text };
  18. empContext.Employees.Add(emp);
  19. empContext.SaveChanges();
  20. }
  21. }
  22. }

The following is the output of the application:

Summary

In this article we saw how to access a SQL Server database with the Entity Framework Code First approach and how to create an index. Happy coding.