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 a delete 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_DeleteApp
  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_DeleteApp
  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_DeleteApp
  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_DeleteApp.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>Email:
  13. </td>
  14. <td>
  15. <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
  16. </td>
  17. </tr>
  18. <tr>
  19. <td colspan="2">
  20. <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
  21. </td>
  22. </tr>
  23. </table>
  24. </div>
  25. </form>
  26. </body>
  27. </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_DeleteApp
  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. var query = (from r in entities.Employees where r.EmployeeDetails.Email == txtEmail.Text select r).Single();
  18. entities.Employees.Remove(query);
  19. entities.SaveChanges();
  20. }
  21. }
  22. }

Output

The output of the application looks like this:

Summary

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