I have two columns called Monkey and Donkey. Both are non-empty, i.e. null
is not allowed. The setup is declared as follows.
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
...
builder.Entity<Thing>()
.Property(a => a.Monkey)
.IsRequired();
builder.Entity<Thing>()
.Property(a => a.Donkey)
.IsRequired();
}
Now, I learned that the combined content must be unique, i.e. no record may have the same monkey and donkey. According to MSDN (index on multiple props), I'm supposed to use HasColumnAnnotation
and pass an instance of IndexAnnotation
. It's also suggested in this answer to use IndexAnnotation.AnnotationName
from System.Data.Entity.Infrastructure.Annotations
.
The problem is that I can't find that method and instead only see HasAnnotation
. It's unclear to me if it's a more recent version (as the post linked to is a bit dated by now). I can't access the namespace Entity in the using
statement, neither.
builder.Entity<Thing>()
.Property(a => a.Monkey)
.HasAnnotation(...);
Is that the right approach or should I continue looking for the other annotation method? What is missing to get the namespace in? Is indexing the appropriate way to go to begin with?
I also tried with index but that requires me to declare a specialized class, which seems clunky and suggests it's the wrong way to go.
builder.Entity<Thing>()
.HasIndex(a => new IndexDedicatedClass(a.Monkey, a.Donkey))
.IsUnique();