Introduction

This article shows how to do table splitting and insert data operations using Code First Approach

SQL Server table structure

table structure

Create a Console application as in the following:

create console application

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

Employeedetails.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Table_Splittling__Insert_Data_CFA
  7. {
  8. public class EmployeeDetails
  9. {
  10. public EmployeeDetails()
  11. {
  12. }
  13. public int EmpId { get; set; }
  14. public string Phone { get; set; }
  15. public string Email { get; set; }
  16. //Navigation Properties
  17. public Employee Employee { get; set; }
  18. }
  19. }

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 Table_Splittling__Insert_Data_CFA
  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. .HasKey(pk => pk.EmpId)
  21. .ToTable("Employees");
  22. modelBuilder.Entity<EmployeeDetails>()
  23. .HasKey(pk => pk.EmpId)
  24. .ToTable("Employees");
  25. modelBuilder.Entity<Employee>()
  26. .HasRequired(p => p.EmployeeDetails)
  27. .WithRequiredPrincipal(c => c.Employee);
  28. base.OnModelCreating(modelBuilder);
  29. }
  30. }
  31. }

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 Table_Splittling__Insert_Data_CFA
  7. {
  8. 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. };
  18. emp.EmployeeDetails = new EmployeeDetails()
  19. {
  20. Phone = "122443",
  21. Email = "[email protected]"
  22. };
  23. empContext.Employees.Add(emp);
  24. empContext.SaveChanges();
  25. Console.WriteLine("Inserted");
  26. Console.ReadKey();
  27. }
  28. }
  29. }

Summary

In this article we saw how to do table splitting and insert data operations using Code First Approach. Happy coding!