5

I have this class:

public class BSC
{
    public int BSCId { get; set; }

    public string BSCName { get; set; }
}

and the config class:

public class BSCConfig :EntityTypeConfiguration<BSC>
{
    public BSCConfig()
    {
        Property(m => m.BSCName).HasMaxLength(50).HasColumnName("Category").IsRequired();

    }
}

I want to make this property Unique, but I do not have isUnique or Index method.

Can you please tell me how to make this property Unique?

Lucian Bumb
  • 2,821
  • 5
  • 26
  • 39

2 Answers2

5

Use HasColumnAnnotation :

Property(m => m.BSCName).HasMaxLength(50).HasColumnName("Category").IsRequired()
  .HasColumnAnnotation("Index",
   new IndexAnnotation(new IndexAttribute("IX_X_Category") { IsUnique = true }));
Taher Rahgooy
  • 6,528
  • 3
  • 19
  • 30
3

You can also do it with data annotations.

[Index("IX_X_Category", 1, IsUnique = true)]
public string BSCName { get; set; }
Hezye
  • 1,521
  • 1
  • 13
  • 15