it:ad:code_first:howto:define_models:unique_indexes

IT:AD:EF/CodeFirst:HowTo:Define Models/Unique Indexes

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 }));
        }
    }
}

  • /home/skysigal/public_html/data/pages/it/ad/code_first/howto/define_models/unique_indexes.txt
  • Last modified: 2023/11/04 02:19
  • by 127.0.0.1