Index Attribute Using Entity Framework

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.   
  8. namespace Index_Attribute___EF  
  9. {  
  10.     public class Employee  
  11.     {  
  12.         public Employee()  
  13.         {  
  14.         }  
  15.   
  16.         [Key]  
  17.         [DatabaseGenerated(DatabaseGeneratedOption.Identity)]  
  18.         public int Id { getset; }  
  19.         public string FirstName { getset; }  
  20.   
  21.         [MaxLength(50)]    
  22.         [Index("IX_LastName_Employee", IsClustered=false)]  
  23.         public string LastName { getset; }  
  24.   
  25.     }  
  26. }
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 Index_Attribute___EF  
  8. {  
  9.     public class EmployeeContext : DbContext  
  10.     {  
  11.         public EmployeeContext()  
  12.             : base("EmployeeConn")  
  13.         {  
  14.             Database.SetInitializer<EmployeeContext>(new CreateDatabaseIfNotExists<EmployeeContext>());  
  15.         }  
  16.   
  17.         public DbSet<Employee> Employees { getset; }  
  18.     }  

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

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.

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