assume that i have an entity like this
public class Example
{
[Key]
public int ExampleId { get; set; }
public string Field1 { get; set; }
public string Field2 { get; set; }
public string OtherField1 { get; set; }
public string OtherField2 { get; set; }
}
I need to use EF code first to create the table.
I need to prevent to add records with duplicate both Field1 and Field2, there is no problem with duplicate OtherField1 and OtherField2. also there is no problem with records those have just Field1 in common but not both Field1 and Field2.
Sure i can check all other records for duplicates but the proper way is to force Database to generate the error.
Thanks to all.
this is how we could create unique key on the column combination using EF
[Index("IX_UniqueFields", 1, IsUnique = true)]
public int Field1 { get; set; }
[Index("IX_UniqueFields", 2, IsUnique = true)]
public int Field2 { get; set; }