0

Has there a way to create a compost unique index in fluente api based on some value ?

Ex:

string code { get; set; }
bool active { get; set; }


Property(x => x.code)
            .HasMaxLength(6)
            .HasColumnAnnotation(
                IndexAnnotation.AnnotationName,
                new IndexAnnotation(new IndexAttribute("IX_CODE", 1) { IsUnique = true }))
            .IsRequired();

Property(x => x.active)
             //I want this only if "active == true"
            .HasColumnAnnotation(
                IndexAnnotation.AnnotationName,
                new IndexAnnotation(new IndexAttribute("IX_CODE", 2) { IsUnique = true }))
            .IsRequired();

This didn't work for me because I want my "code" to be unique only when my "active" is true, otherwise I can have duplicated codes (with active == false).

Anyone knows a way ?

Lucas Freitas
  • 1,016
  • 4
  • 16
  • 37

1 Answers1

1

I am afraid you cannot do that. However, you can create two different types using TPH and then apply different rules to each one.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64