You will have to provide a Context Initializer and override Seed method to initialize your database. There you can add an Unique Index using DbContext.Database.ExecuteSqlCommand. Here is the sample code for the same.
Model
public class Relay {
//Scalar Properties
private int _id;
[Key]
public virtual int Id {
get { return _id; }
set { SetField(ref _id, value, () => Id); }
}
private string _name;
[MaxLength(100)]
[Required]
public virtual string Name {
get { return _name; }
set { SetField(ref _name, value, () => Name); }
}
//Other properties of Model
}
DBContext
public class RelayContext : DbContext {
public DbSet<Relay> Relays { get; set; }
public RelayContext(string connectionString) :
base(connectionString) {
Database.SetInitializer(new RelayContextInitializer());
}
}
ContextInitializer
public class RelayContextInitializer : CreateDatabaseIfNotExists<RelayContext> {
protected override void Seed(RelayContext context) {
context.Database.ExecuteSqlCommand("CREATE UNIQUE INDEX Uix_Relay_Name ON Relays(Name)");
}
}
That's it. You have now an Unique Index created on Name field of Relays Table.