Introduction

This article shows how to access a SQL Server database with Entity Framework code first approach and later we will also look at how to do an insert data operation using a complex type.

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_InsertApp
  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_InsertApp
  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_InsertApp
  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_InsertApp.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>FirstName:
  13. </td>
  14. <td>
  15. <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
  16. </td>
  17. </tr>
  18. <tr>
  19. <td>LastName:
  20. </td>
  21. <td>
  22. <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
  23. </td>
  24. </tr>
  25. <tr>
  26. <td>Age:
  27. </td>
  28. <td>
  29. <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
  30. </td>
  31. </tr>
  32. <tr>
  33. <td>Phone:
  34. </td>
  35. <td>
  36. <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
  37. </td>
  38. </tr>
  39. <tr>
  40. <td>Email:
  41. </td>
  42. <td>
  43. <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
  44. </td>
  45. </tr>
  46. <tr>
  47. <td colspan="2">
  48. <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
  49. </td>
  50. </tr>
  51. </table>
  52. </div>
  53. </form>
  54. </body>
  55. </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_InsertApp
  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. }
  15. protected void btnSave_Click(object sender, EventArgs e)
  16. {
  17. Employee emp = new Employee()
  18. {
  19. FirstName = txtFirstName.Text
  20. ,LastName = txtLastName.Text
  21. };
  22. emp.EmployeeDetails = new EmployeeDetails()
  23. {
  24. Age = int.Parse(txtAge.Text)
  25. ,Phone = int.Parse(txtPhone.Text)
  26. ,Email = txtEmail.Text
  27. };
  28. entities.Employees.Add(emp);
  29. entities.SaveChanges();
  30. }
  31. }
  32. }

Output

The output of the application looks like this:

Summary

In this article we saw how to access a SQL Server database using the Entity Framework code first approach and how to do an insert data operation using a complex type. Happy coding.