Entity Splitting Insert Data Using Code First Approach

Introduction

This article shows how to perform Entity Splitting and later we will also look at how to perform an insert data operation using the Code First Approach.

Create a console application as in the following:

 

Employee.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.ComponentModel.DataAnnotations.Schema;  
  5. using System.Linq;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8.   
  9. namespace EntitySplitting_CFA_Insert  
  10. {  
  11.    public class Employee  
  12.     {  
  13.         public Employee()  
  14.         {  
  15.         }  
  16.   
  17.         [Key]  
  18.         [DatabaseGenerated(DatabaseGeneratedOption.Identity)]  
  19.         public int Id { getset; }  
  20.         public string FirstName { getset; }  
  21.         public string LastName { getset; }  
  22.         public int Phone { getset; }  
  23.         public string Email { getset; }  
  24.     }  
  25. }

Employeecontext.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data.Entity;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.   
  8. namespace EntitySplitting_CFA_Insert  
  9. {  
  10.     public class EmployeeContext : DbContext  
  11.     {  
  12.         public EmployeeContext()  
  13.             : base("EmployeeConn")  
  14.         {  
  15.             Database.SetInitializer<EmployeeContext>(new CreateDatabaseIfNotExists<EmployeeContext>());  
  16.         }  
  17.   
  18.         public DbSet<Employee> Employees { getset; }  
  19.   
  20.         protected override void OnModelCreating(DbModelBuilder modelBuilder)  
  21.         {  
  22.             modelBuilder.Entity<Employee>()  
  23.                 .Map(map =>  
  24.                 {  
  25.                     map.Properties(r => new  
  26.                     {  
  27.                         r.Id,  
  28.                         r.FirstName,  
  29.                         r.LastName  
  30.                     }); map.ToTable("Employee");  
  31.                 })  
  32.                 .Map(map =>  
  33.                 {  
  34.                     map.Properties(r => new  
  35.                     {  
  36.                         r.Phone,  
  37.                         r.Email  
  38.                     }); map.ToTable("EmployeeDetails");  
  39.                 });  
  40.   
  41.             base.OnModelCreating(modelBuilder);  
  42.         }  
  43.     }  
  44. }

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>  
Program.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace EntitySplitting_CFA_Insert  
  8. {  
  9.     public class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             EmployeeContext empContext = new EmployeeContext();  
  14.             Employee emp = new Employee()  
  15.             {  
  16.                 FirstName = "Haney",  
  17.                 LastName = "Jow",  
  18.                 Phone = 12345678,  
  19.                 Email="[email protected]"  
  20.   
  21.             };  
  22.             empContext.Employees.Add(emp);  
  23.             empContext.SaveChanges();  
  24.             Console.WriteLine("Inserted");  
  25.             Console.ReadKey();  
  26.         }  
  27.     }  
  28. }  
The output of the application looks as in the following:

 
 
 

Summary

In this article we saw how to perform Entity Splitting and how to perform an insert data operation using the Code First Approach. Happy coding!


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