# IT:AD:EF/CodeFirst:HowTo:Define Models/Unique Indexes #
* [[../|(UP)]]
{{indexmenu>.#2|nsort tsort}}
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
{
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 }));
}
}
}
## Resources ##
* http://stackoverflow.com/questions/21573550/entity-framework-6-setting-unique-constraint-with-fluent-api
* http://blog.oneunicorn.com/2014/02/15/ef-6-1-creating-indexes-with-indexattribute/