I have a two columns in database that are set as primary key let's say Code1 and Code2. I use Entity Framework fluent api to create key like this:
entity.HasKey(p => new { p.Code1, p.Code2 }).HasName("table_pkey");
What I need is for key to be in lexicographical order. So for example when I have a record in database.
var code1 = "AA01";
var code2 = "AB01";
var pkey = "AA01AB01";
and I try to add
var code1 = "AB01";
var code2 = "AA01";
var pkey = "AB01AA01";
I want this to be regarded as a duplicate key for database so it won't allow to add them.
Thank you for your help
'AA01AB01'and'AB01AA01'are not equal to each other, so you can't enforce a primary key to be violated here. It seems that what you actually needed here is 2 contraints; the 1st to ensure the value ofCode1is less thanCode2and then the primary key constraint. In T-SQl, this would be defined as the following:In your application, you likely want to check that
Code1is also less thanCode2and "swap" the values if they aren't.