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

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.