0

I have an entity:

Item{
   Id,
   Barcode,
   UnitBarcode
}

I wand both columns 'Barcode' and 'UnitBarcode' be unique, that there are no equal'UnitBarcode' and 'Barcode' in any rows, like this table below:

Id Barcode UnitBarcode status
1 123 1234 allowed
2 1234 5687 not allowed
3 5896 123 not allowed
4 123 798965 not allowed

Is there a way to implement this in EF Core 5.0 with Sql Server?

jps
  • 20,041
  • 15
  • 75
  • 79
Nahro Fuad
  • 751
  • 1
  • 5
  • 5
  • 3
    You simply want to add a UNIQUE constraint to a PAIR of columns, like this: `ALTER TABLE dbo.yourtablename ADD CONSTRAINT uq_yourtablename UNIQUE(column1, column2);`: https://stackoverflow.com/a/15800279/421195, or a unique index, like this: https://stackoverflow.com/a/39021965/421195 – paulsm4 Jul 03 '21 at 20:56
  • 1
    https://learn.microsoft.com/es-es/ef/core/modeling/keys?tabs=data-annotations How to set keys in EF – Nikasha Von carstein Jul 03 '21 at 22:45

0 Answers0