简体   繁体   English

如何使用 EF Core fluent api 创建多个索引?

[英]How to create more than one index using EF Core fluent api?

strangely I couldn't find any example or mention of this anywhere.奇怪的是,我在任何地方都找不到任何例子或提及此事。 I would like to set indexes on two of my columns (not a composite index, just two separate indexes) using the fluent api.我想使用 fluent api 在我的两个列上设置索引(不是复合索引,只是两个单独的索引)。

For example, I was wondering if it's possible to do something like:例如,我想知道是否可以执行以下操作:

modelBuilder.Entity<T>
            .HasIndex(h => h.Column1)
            .HasIndex(h => h.Column2);

However, from the looks of it, it's not possible to chain indexes like this.但是,从它的外观来看,不可能像这样链接索引。 Currently what I'm doing is setting them separately like:目前我正在做的是分别设置它们,如:

modelBuilder.Entity<T>
            .HasIndex(h => h.Column1)

modelBuilder.Entity<T>
            .HasIndex(h => h.Column2)

Is there a better way to do this?有一个更好的方法吗?

HasIndex() returns a type of IndexBuilder which allows you to call things like .IsUnique() or .HasName() etc. HasIndex() 返回一种 IndexBuilder 类型,它允许您调用 .IsUnique() 或 .HasName() 等。

Is there a better way to do this?有一个更好的方法吗?

Depends if you consider this better or not, and if you REALLY want to be fluent.取决于您是否认为这更好,以及您是否真的想要流利。

To keep adding indexes using a fluent style you need to get back to the EntityTypeBuilder.要使用流畅的样式继续添加索引,您需要回到 EntityTypeBuilder。 If you really wanted you could use an extension method.如果你真的想要,你可以使用扩展方法。

modelBuilder.Entity<T>
            .AddIndex(h => h.Column1)
            .AddIndex(h => h.Column2);

public static EntityTypeBuilder<TEntity> AddIndex<TEntity>(this EntityTypeBuilder<TEntity> builder, Expression<Func<TEntity, object>> indexExpression) where TEntity : class
{
    builder.HasIndex(indexExpression);
    return builder;
}

Or或者

builder.Entity<T>()
    .AddIndex(indexBuilder => indexBuilder.HasIndex(h => h.Column1))
    .AddIndex(indexBuilder => indexBuilder.HasIndex(h => h.Column2).IsUnique());

public static EntityTypeBuilder<TEntity> AddIndex<TEntity>(this EntityTypeBuilder<TEntity> builder, Action<EntityTypeBuilder<TEntity>> action) where TEntity : class
{
    action(builder);
    return builder;
}

In Entity Framework 6.x you can create indexes using both Data Annotation and Fluent API but in EF Core according to EF Core Indexes documentation, so far, you can only create indexes with Fluent API .Entity Framework 6.x您可以使用Data AnnotationFluent API创建索引,但根据EF Core Indexes文档,在EF Core ,到目前为止,您只能使用Fluent API创建索引。 So what you are doing is the appropriate way of doing this in EF Core .因此,您正在做的是在EF Core中执行此操作的适当方法。

Additionally one thing you can do is separate your Entity Configuration from DbContext as follows:此外,您可以做的一件事是将实体配置与DbContext ,如下所示:

public class FooConfiguration : IEntityTypeConfiguration<Foo>
{
    public void Configure(EntityTypeBuilder<Foo> builder)
    {
        ...
        builder.HasIndex(h => h.Column1).IsUnique();
        builder.HasIndex(h => h.Column2).IsUnique();
       ..
    }
}

Then apply the configuration in OnModelCreating method as follows:然后在OnModelCreating方法中应用配置如下:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.ApplyConfiguration(new FooConfiguration()); //<-- add here
}

You can create multiple index using Fluent Api您可以使用 Fluent Api 创建多个索引

 Add multiple columns index:
 modelBuilder.Entity<MyEntity>().HasIndex(p => new {p.Prop1, p.Prop2});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM