0

I would like to know if someone knows how to make a table with a primary key composed of two columns, where the first column is sent by me, and the second is generated from the first

public class Person
{
    public int idPerson { get; set; }
    public string name { get; set; }
}

public class PersonAdress
{
    public int idPerson { get; set; }
    public int DireccionId { get; set; }
    public string reference { get; set; }
}

I am looking for the incremental of the second column to be if the first column changes

RESULT

Ravi
  • 73
  • 5

1 Answers1

2

how to make a table with a primary key composed of two columns

You can add the following code by fluent api in dbContext's OnModelCreating method :

 modelBuilder.Entity<PersonAdress>().HasKey(sc => new { sc.idPerson , sc.DireccionId });

You can also have a reference for this.

LouraQ
  • 6,443
  • 2
  • 6
  • 16
  • Thank you, but this solution increments the AddressId column (1.1) and wanted the increment to be when IdPerson is different, such as the row number per IdPerson column – Francisco Martinez Oct 09 '20 at 22:42
  • @FranciscoMartinez, what's the `AddressId ` column here? Is it also in PersonAdress table? If so, according to your rules, if AddressId must always be consistent with idPerson, then why do you need AddressId? You can directly operate on idPerson. – LouraQ Oct 14 '20 at 02:37