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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace ComplexType_CFA_DeleteApp
- {
- public class Employee
- {
- public Employee()
- {
- }
- public int Id { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public EmployeeDetails EmployeeDetails { get; set; }
- }
- }
EmployeeDetails.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations.Schema;
- using System.Linq;
- using System.Web;
- namespace ComplexType_CFA_DeleteApp
- {
- [ComplexType]
- public class EmployeeDetails
- {
- public EmployeeDetails()
- {
- }
- public int Phone { get; set; }
- public int Age { get; set; }
- public string Email { get; set; }
- }
- }
Employeecontext.cs
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Web;
- namespace ComplexType_CFA_DeleteApp
- {
- public class EmployeeContext: DbContext
- {
- public EmployeeContext()
- : base("EmployeeConn")
- {
- Database.SetInitializer<EmployeeContext>(new CreateDatabaseIfNotExists<EmployeeContext>());
- }
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- //Set primary key to Employee table
- modelBuilder.Entity<Employee>().HasKey(m => m.Id).Property(m => m.Id).IsRequired();
- //First Name is required and Max Length is 50
- modelBuilder.Entity<Employee>().Property(p => p.FirstName).IsRequired().HasMaxLength(50);
- //Last Name is required and Max Length is 50
- modelBuilder.Entity<Employee>().Property(p => p.LastName).IsRequired().HasMaxLength(50);
- //Age is required
- modelBuilder.ComplexType<EmployeeDetails>().Property(p => p.Age).IsRequired();
- //Phone is required
- modelBuilder.ComplexType<EmployeeDetails>().Property(p => p.Phone).IsRequired();
- //Email is required
- modelBuilder.ComplexType<EmployeeDetails>().Property(p => p.Email).IsRequired();
- }
- public DbSet<Employee> Employees { get; set; }
- }
- }
Web.config
- <connectionStrings>
- <add name="EmployeeConn"
- connectionString="Data Source=WIN-B4KJ8JI75VF;Initial Catalog=EmployeeDB;Integrated Security=true"
- providerName="System.Data.SqlClient"/>
- </connectionStrings>
Webform1.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ComplexType_CFA_DeleteApp.WebForm1" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td>Email:
- </td>
- <td>
- <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
Webform1.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace ComplexType_CFA_DeleteApp
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- EmployeeContext entities = new EmployeeContext();
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void btnSave_Click(object sender, EventArgs e)
- {
- var query = (from r in entities.Employees where r.EmployeeDetails.Email == txtEmail.Text select r).Single();
- entities.Employees.Remove(query);
- entities.SaveChanges();
- }
- }
- }
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.

Sonu ChaudharyPosted May 26, 2016, 10:48 AM
Good one...
Santhakumar MunuswamyPosted May 12, 2015, 12:31 AM
Thanks for nice article
NitinPosted May 11, 2015, 12:21 PM
good one..thanks for sharing