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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.