IT:AD:EF/CodeFirst:HowTo:Define Models/Unique Indexes
Summary
The following was possible before EF6.1 which came out in March 2014.
Process
Define a Map, using HasColumnAnnotation to add Index to n columns (note the numbering…)
namespace XAct.Spikes.CodeFirst
{
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;
public class SerializedSettingMap : EntityTypeConfiguration<SerializedSetting>
{
public SerializedSettingMap()
{
this.ToTable("Settings");
this.HasKey(x => x.Id);
this.Property(x => x.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(x => x.Env)
.IsRequired()
.HasMaxLength(32)
.HasColumnAnnotation(
"Index",
new IndexAnnotation(
new IndexAttribute("IX_EnvAppZoneHostKey", 1) { IsUnique = true }));
this.Property(x => x.App)
.IsRequired()
.HasMaxLength(32)
.HasColumnAnnotation(
"Index",
new IndexAnnotation(
new IndexAttribute("IX_EnvAppZoneHostKey", 2) { IsUnique = true }));
this.Property(x => x.Zone)
.IsRequired()
.HasMaxLength(32)
.HasColumnAnnotation(
"Index",
new IndexAnnotation(
new IndexAttribute("IX_EnvAppZoneHostKey", 3) { IsUnique = true }));
this.Property(x => x.Host)
.IsRequired()
.HasMaxLength(32)
.HasColumnAnnotation(
"Index",
new IndexAnnotation(
new IndexAttribute("IX_EnvAppZoneHostKey", 4) { IsUnique = true }));
this.Property(x => x.Key)
.IsRequired()
.HasMaxLength(128)
.HasColumnAnnotation(
"Index",
new IndexAnnotation(
new IndexAttribute("IX_EnvAppZoneHostKey", 5) { IsUnique = true }));
}
}
}