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 we can perform select data operation using complex type.

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. namespace ComplexType_CFA_SelectApp
  6. {
  7. public class Employee
  8. {
  9. public Employee()
  10. {
  11. }
  12. public int Id { get; set; }
  13. public string FirstName { get; set; }
  14. public string LastName { get; set; }
  15. public EmployeeDetails EmployeeDetails { get; set; }
  16. }
  17. }

EmployeeDetails.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations.Schema;
  4. using System.Linq;
  5. using System.Web;
  6. namespace ComplexType_CFA_SelectApp
  7. {
  8. [ComplexType]
  9. public class EmployeeDetails
  10. {
  11. public EmployeeDetails()
  12. {
  13. }
  14. public int Phone { get; set; }
  15. public int Age { get; set; }
  16. public string Email { get; set; }
  17. }
  18. }

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

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="ComplexType_CFA_SelectApp.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. <asp:GridView ID="GridView1" runat="server"></asp:GridView>
  11. </div>
  12. </form>
  13. </body>
  14. </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 ComplexType_CFA_SelectApp
  8. {
  9. public partial class WebForm1 : System.Web.UI.Page
  10. {
  11. EmployeeContext entities = new EmployeeContext();
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. var data = from x in entities.Employees
  15. select new
  16. {
  17. EmpId = x.Id,
  18. Firstname = x.FirstName,
  19. Lastname = x.LastName,
  20. Age = x.EmployeeDetails.Age,
  21. Email = x.EmployeeDetails.Email,
  22. Phone= x.EmployeeDetails.Phone
  23. };
  24. GridView1.DataSource = data.ToList();
  25. GridView1.DataBind();
  26. }
  27. }
  28. }

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 using complex type. Happy coding!