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

entity framework core index attribute
ef core index include columns
ef core fluent api hasindex
entity framework core filtered index
ef core index descending
ef core clustered index
ef core 3 index attribute
efcore composite index

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.

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.

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. 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;
}

Indexes, You may wish to create indexes on other properties to speed up data retrieval: public class SampleContext : DbContext. { public DbSet<Book> Books { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder. Entity<Book>() . HasIndex(b => b. Isbn); } 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. So what you are doing is the appropriate way of doing this in EF Core.

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. So what you are doing is the appropriate way of doing this in EF Core.

Additionally one thing you can do is separate your Entity Configuration from DbContext as follows:

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:

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

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

The Fluent API HasIndex Method, However, in the "Contact" entity I need to create a unique index Fluent Api - Mapping an index over more than one column across entity EF Core version: Support of unique Index with Owned types properties #12637. EF Core only supports one index per distinct set of properties. If you use the Fluent API to configure an index on a set of properties that already has an index defined, either by convention or previous configuration, then you will be changing the definition of that index.

You can create multiple index using Fluent Api

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

Fluent Api, I have a MVC ASP.NET application using Entity Framework 6 - Code First approach. Using the Fluent API, how can I add an index on multiple´┐Ż EF Fluent API is based on a Fluent API design pattern (a.k.a Fluent Interface) where the result is formulated by method chaining. In Entity Framework Core, the ModelBuilder class acts as a Fluent API. By using it, we can configure many different things, as it provides more configuration options than data annotation attributes.

[SOLVED], EF can you make a Mutli-column Index using shadow properties? c# ef-fluent-api entity-framework entity-framework-core sqlite. Fluent API; Data annotations; To configure a relationship in the Fluent API, you start by identifying the navigation properties that make up the relationship. HasOne or HasMany identifies the navigation property on the entity type you are beginning the configuration on. You then chain a call to WithOne or WithMany to identify the inverse

[SOLVED], In Entity Framework Core, the ModelBuilder class acts as a Fluent API. By using it , we can configure many different things, as it provides more configuration Index, table name, one-to-one, one-to-many, many-to-many relationships etc. Fluent API configuration also facilitates cleaner code, in that the configuration can be kept separate from the domain classes. Separate Configuration Classes Most examples in this guide show configurations being applied in the OnModelCreating method, but it is recommended to separate configurations out to individual files per entity

Fluent API in Entity Framework Core, The Fluent API can be used to configure properties of an entity to map it with a db column. The default Code-First convention creates a column for a property with the same Property(expression) allows you to use different methods to configure a Learn Entity Framework DB-First, Code-First and EF Core step by step. Generally, you don't need to configure one-to-one relationships manually because EF Core includes Conventions for One-to-One Relationships. However, if the key or foreign key properties do not follow the convention, then you can use data annotation attributes or Fluent API to configure a one-to-one relationship between the two entities.