В отсутствие встроенной поддержки, вы может использовать пользовательский атрибут вашего собственного аннотировать свойства модели и применять в OnModelCreating()
:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
foreach (var prop in entity.GetProperties())
{
var attr = prop.PropertyInfo.GetCustomAttribute<IndexAttribute>();
if (attr != null)
{
var index = entity.AddIndex(prop);
index.IsUnique = attr.IsUnique;
index.SqlServer().IsClustered = attr.IsClustered;
}
}
}
}
с помощью простого класса атрибута маркера:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class IndexAttribute : Attribute
{
public bool IsUnique { get; set; }
public bool IsClustered { get; set; }
}
Затем в классе модели, просто добавьте атрибут, чтобы создать вторичный индекс:
public class User
{
public int UserId { get; set; }
[Index(IsUnique = true, IsClustered = true)]
public string Nickname { get; set; }
}
Образцы свободно Апи для индексов см в https://www.learnentityframeworkcore.com/configuration/fluent- api/hasindex-method –