如何首先在EF6代码中设置唯一的属性

Luc*_*umb 5 c# entity-framework entity-framework-6

我有这个课:

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

    public string BSCName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和配置类:

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

    }
}
Run Code Online (Sandbox Code Playgroud)

我想使此属性唯一,但是我没有isUnique或Index方法。

您能告诉我如何使此属性唯一吗?

Tah*_*ooy 5

使用HasColumnAnnotation

Property(m => m.BSCName).HasMaxLength(50).HasColumnName("Category").IsRequired()
  .HasColumnAnnotation("Index",
   new IndexAnnotation(new IndexAttribute("IX_X_Category") { IsUnique = true }));
Run Code Online (Sandbox Code Playgroud)