Delete Data From Complextype Using Code First Approach

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

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.   
  7. namespace ComplexType_CFA_DeleteApp  
  8. {  
  9.     [ComplexType]  
  10.     public class EmployeeDetails  
  11.     {  
  12.         public EmployeeDetails()  
  13.         {  
  14.         }  
  15.   
  16.         public int Phone { getset; }  
  17.         public int Age { getset; }  
  18.         public string Email { getset; }  
  19.     }  
  20. }  

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

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.   
  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>Email:  
  15.                     </td>  
  16.                     <td>  
  17.                         <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>  
  18.                     </td>  
  19.                 </tr>  
  20.                 <tr>  
  21.                     <td colspan="2">  
  22.                         <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />  
  23.                     </td>  
  24.                 </tr>  
  25.             </table>  
  26.         </div>  
  27.     </form>  
  28. </body>  
  29. </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 ComplexType_CFA_DeleteApp  
  9. {  
  10.     public partial class WebForm1 : System.Web.UI.Page  
  11.     {  
  12.         EmployeeContext entities = new EmployeeContext();  
  13.         protected void Page_Load(object sender, EventArgs e)  
  14.         {  
  15.   
  16.         }  
  17.   
  18.         protected void btnSave_Click(object sender, EventArgs e)  
  19.         {  
  20.             var query = (from r in entities.Employees where r.EmployeeDetails.Email == txtEmail.Text select r).Single();  
  21.             entities.Employees.Remove(query);  
  22.             entities.SaveChanges();  
  23.         }  
  24.     }  
  25. }

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.


Similar Articles
MVC Corporation
MVC Corporation is consulting and IT services based company.