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. namespace EntitySplitting_CFA_Insert
  9. {
  10. public class Employee
  11. {
  12. public Employee()
  13. {
  14. }
  15. [Key]
  16. [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  17. public int Id { get; set; }
  18. public string FirstName { get; set; }
  19. public string LastName { get; set; }
  20. public int Phone { get; set; }
  21. public string Email { get; set; }
  22. }
  23. }

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. namespace EntitySplitting_CFA_Insert
  8. {
  9. public class EmployeeContext : DbContext
  10. {
  11. public EmployeeContext()
  12. : base("EmployeeConn")
  13. {
  14. Database.SetInitializer<EmployeeContext>(new CreateDatabaseIfNotExists<EmployeeContext>());
  15. }
  16. public DbSet<Employee> Employees { get; set; }
  17. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  18. {
  19. modelBuilder.Entity<Employee>()
  20. .Map(map =>
  21. {
  22. map.Properties(r => new
  23. {
  24. r.Id,
  25. r.FirstName,
  26. r.LastName
  27. }); map.ToTable("Employee");
  28. })
  29. .Map(map =>
  30. {
  31. map.Properties(r => new
  32. {
  33. r.Phone,
  34. r.Email
  35. }); map.ToTable("EmployeeDetails");
  36. });
  37. base.OnModelCreating(modelBuilder);
  38. }
  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>
Program.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace EntitySplitting_CFA_Insert
  7. {
  8. public class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. EmployeeContext empContext = new EmployeeContext();
  13. Employee emp = new Employee()
  14. {
  15. FirstName = "Haney",
  16. LastName = "Jow",
  17. Phone = 12345678,
  18. Email="[email protected]"
  19. };
  20. empContext.Employees.Add(emp);
  21. empContext.SaveChanges();
  22. Console.WriteLine("Inserted");
  23. Console.ReadKey();
  24. }
  25. }
  26. }
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!