Is there a way to create an index on a property/column using code-first, instead of using the new IndexAttribute ?
I write an extension method for use in fluent EF to avoid extra code:
public static PrimitivePropertyConfiguration HasIndexAnnotation(
this PrimitivePropertyConfiguration primitivePropertyConfiguration,
IndexAttribute indexAttribute = null
)
{
indexAttribute = indexAttribute ?? new IndexAttribute();
return primitivePropertyConfiguration
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(indexAttribute)
);
}
then use it like this:
Property(t => t.CardNo)
.HasIndexAnnotation();
or like this if index needs some configs:
Property(t => t.CardNo)
.HasIndexAnnotation(new IndexAttribute("IX_Account") { IsUnique = true });