Introduction

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

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_Splitting_Update_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_Splitting_Update_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_Splitting_Update_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_Splitting_Update_Data_CFA
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. EmployeeContext empContext = new EmployeeContext();
  13. int empId = 1;
  14. var emp = empContext.Employees.Include("EmployeeDetails").Where(a => a.EmpId.Equals(empId)).SingleOrDefault();
  15. emp.FirstName = "Jonny";
  16. emp.EmployeeDetails.Email = "[email protected]";
  17. empContext.SaveChanges();
  18. }
  19. }
  20. }

The output of this application is as in the following.

Before SQL update

before SQL update

After SQL update

after SQL update

Summary

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