Manage Primary Key And Foreign Key Relationship During Migration

Introduction

 
In this article, we will learn how to create models for database table migration. When we build a simple database for our project, then it is easy to build class models. But when we develop a complex database for our project, then it's hard to maintain PK & FK relationship over the model. It is not easy to maintain, the string length and datatypes over the model.
 
So, let's see a few points while creating a model class.
 
Okay, here are the three tables, names - UserDetails, Customer, and Order. Here, we want to create the ID column as the primary key and a foreign key to the UserId column with the AspNetUser table. Like in the database here, also we want to restrict columns with string length. And also want to create two primary keys in a single model.
like below,
 
 Manage Primary Key And Foreign Key Relationship During Migration  Manage Primary Key And Foreign Key Relationship During Migration
 
 Manage Primary Key And Foreign Key Relationship During Migration
 
Here, Firstly I am going to create the UserDetails table. In which few properties and a foreign key relationship exit with the AspnetUser table.
  1. public class UserDetails {  
  2.     [Key]  
  3.     public int Id {  
  4.         get;  
  5.         set;  
  6.     }  
  7.     [StringLength(100)]  
  8.     [Column(TypeName = "varchar(100)")]  
  9.     public string Name {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     [StringLength(50)]  
  14.     [Column(TypeName = "varchar(50)")]  
  15.     public string Email {  
  16.         get;  
  17.         set;  
  18.     }  
  19.     [StringLength(1)]  
  20.     [Column(TypeName = "char(1)")]  
  21.     public string Gender {  
  22.         get;  
  23.         set;  
  24.     }  
  25.     public string ProfilePic {  
  26.         get;  
  27.         set;  
  28.     }  
  29.     [ForeignKey("IdentityUser")]  
  30.     public string UserId {  
  31.         get;  
  32.         set;  
  33.     }  
  34.     public virtual IdentityUser IdentityUser {  
  35.         get;  
  36.         set;  
  37.     }  
  38.     public byte Role {  
  39.         get;  
  40.         set;  
  41.     }  
  42.     public DateTime ? CreatedOn {  
  43.         get;  
  44.         set;  
  45.     }  
  46.     public bool ? IsActive {  
  47.         get;  
  48.         set;  
  49.     }  
  50. }   
Here are a few packages for model attribute properties, which we will use to decorate class properties.
  1. using System.ComponentModel.DataAnnotations;  
  2. using System.ComponentModel.DataAnnotations.Schema;  
In this model, we are making a Foreign Key relationship with IdentityUser class, which is available in the database with the name AspnetUsers.
  1. using Microsoft.AspNetCore.Identity;  
If we want to set the string length of 100/50 or something else varchar characters in the database of the Name property, then we define attribute like this,
  1. [StringLength(100)]    
  2. [Column(TypeName = "varchar(100)")]    
  3. public string Name { get; set; }    
If we desire to set the string length of 1 character only in the database of the Gender property, then we define attribute like this,
  1. [StringLength(1)]    
  2. [Column(TypeName = "char(1)")]    
  3. public string Gender { get; set; }   
If we desire to set the DateTime datatype with nullable constraint in the database of the CreatedOn property, then we define attribute like this,
  1. public DateTime? CreatedOn { get; set; }  
If we desire to make a foreign Key relationship with IdentityUser and store the IdentityUser table Id in the UserId column, then we define attribute like this,
  1. [ForeignKey("IdentityUser")]  
  2. public string UserId { get; set; }  
  3. public virtual IdentityUser IdentityUser { get; set; }  
Now, we will create the customer table. Which contains FullName, Description & Address properties, and the one foreign Key relationship with UserDetails table in the UserDetailsId column.
  1. public class Customer {  
  2.     [Key]  
  3.     public int Id {  
  4.         get;  
  5.         set;  
  6.     }  
  7.     [StringLength(100)]  
  8.     [Column(TypeName = "varchar(100)")]  
  9.     public string FullName {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     [StringLength(500)]  
  14.     [Column(TypeName = "varchar(500)")]  
  15.     public string Description {  
  16.         get;  
  17.         set;  
  18.     }  
  19.     [StringLength(500)]  
  20.     [Column(TypeName = "varchar(500)")]  
  21.     public string Address {  
  22.         get;  
  23.         set;  
  24.     }  
  25.     [ForeignKey("UserDetails")]  
  26.     public virtual int UserDetailsId {  
  27.         get;  
  28.         set;  
  29.     }  
  30.     public virtual UserDetails UserDetails {  
  31.         get;  
  32.         set;  
  33.     }  
  34. }   
If we wish to create two foreign Keys in a single table like UserDetailsId, CustomerId in the Order table, then we write two properties for a single foreign key like this,
  1. public class Order  
  2.    {  
  3.        [Key]  
  4.        public int Id { get; set; }  
  5.   
  6.        [StringLength(100)]  
  7.        [Column(TypeName = "varchar(100)")]  
  8.        public string OrderNumber { get; set; }  
  9.   
  10.        [ForeignKey("Customer")]  
  11.        public virtual int CustomerId { get; set; }  
  12.        public virtual Customer Customer { get; set; }  
  13.   
  14.        [ForeignKey("UserDetails")]  
  15.        public virtual int UserDetailsId { get; set; }  
  16.        public virtual UserDetails UserDetails { get; set; }  
  17.    }  
Register all tables in ApplicationDbContext class set the proper connection string in appsettings.json, register properly this class to Startup.cs class, and then run migration by migration command.
 
ApplicationDbContex.cs class
  1. public class ApplicationDbContext: IdentityDbContext {  
  2.     public ApplicationDbContext(DbContextOptions < ApplicationDbContext > options): base(options) {}  
  3.     public DbSet < UserDetails > UserDetails {  
  4.         get;  
  5.         set;  
  6.     }  
  7.     public DbSet < Customer > Customer {  
  8.         get;  
  9.         set;  
  10.     }  
  11.     public DbSet < Order > Order {  
  12.         get;  
  13.         set;  
  14.     }  
  15. }   
appsettings.json class
  1. "ConnectionStrings": {  
  2.    "DefaultConnection""Server=MyServer;Database=db_A;user=sa;password=admin@1234;Trusted_Connection=False;"  
  3.  },  
Startup.cs class 
  1. public void ConfigureServices(IServiceCollection services) {  
  2.     services.AddDbContext < ApplicationDbContext > (options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));  
  3.     services.AddDefaultIdentity < IdentityUser > (options => options.SignIn.RequireConfirmedAccount = true).AddEntityFrameworkStores < ApplicationDbContext > ();  
  4.     services.AddControllersWithViews();  
  5.     services.AddRazorPages();  
  6. }   

Summary

 
In this article, we learned the concepts of creating model classes with the Pk & Fk relationship. Apart from this, we saw to maintain data types and data length with a class decorative attribute.


Similar Articles