entity framework 2 values primary key code example

Example 1: The entity type has multiple properties with the [Key] attribute.

public class NorthwindContext : DbContext
{
     public NorthwindContext(DbContextOptions<NorthwindContext> options):base(options) { }

     public NorthwindContext() { }

     protected override void OnModelCreating(ModelBuilder builder)
     {
         builder.Entity<Relationship>().HasKey(table => new {
         table.FriendId, table.UserId
         });
     }
     public DbSet<Relationship> Relationships { get; set; }
     public DbSet<User> Users { 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; }}