I want to enforce Unique constraint in a table & I am using Entity Framework Code-First.
Is it possible to add a unique constraint using EF 6 as i believe in ear
Use this to define it in config class, if you're avoiding using annotations:
public class YourTableConfig : EntityTypeConfiguration
{
public YourTableConfig()
{
ToTable("YourTableDbName");
HasKey(u => u.Id);
Property(c => c.CompanyId).HasColumnType("nvarchar").HasMaxLength(9).IsRequired();
HasIndex(x => x.CompanyId).IsUnique(); // This sets the unique index
}
}