ef core alternate key code example

Example 1: define alternate ke in ef core

public class SampleContext : DbContext{    public DbSet<Applicant> Applicants { get; set; }        protected override void OnModelCreating(ModelBuilder modelBuilder)    {        modelBuilder.Entity<Applicant>()            .HasAlternateKey(a => a.PassportNumber);    }      }public class Applicant{    public int ApplicantId { get; set; }    public string Name { get; set; }    public string PassportNumber { get; set; }}

Example 2: define alternate ke in ef core

public class SampleContext : DbContext{    public DbSet<Applicant> Applicants { get; set; }        protected override void OnModelCreating(ModelBuilder modelBuilder)    {        modelBuilder.Entity<Applicant>()            .HasAlternateKey(a => new { a.PassportNumber, a.CountryCode });    } }public class Applicant{    public int ApplicantId { get; set; }    public string Name { get; set; }    public string PassportNumber { get; set; }    public string CountryCode { get; set; }}