Oleg Sh
Oleg Sh

Reputation: 9013

EF Fluent API - HasIndex for complex field

I need to use Fluent API (no DataAnnotation) and should create complex Index (3 fields). I try to do it:

        this.HasIndex(p => p.LoadId).HasName("IX_UniqueLoad").IsUnique();
        this.HasIndex(p => p.LocationId).HasName("IX_UniqueLoad").IsUnique();
        this.HasIndex(p => p.StopAction).HasName("IX_UniqueLoad").IsUnique();

but it says:

The index with name 'IX_UniqueLoad' on table 'dbo.Stop' has the same column order of '-1' specified for columns 'LoadId' and 'LocationId'. Make sure a different order value is used for the IndexAttribute on each column of a multi-column index.

How to do it?

Upvotes: 0

Views: 465

Answers (1)

Oleg Sh
Oleg Sh

Reputation: 9013

I found solution:

this.HasIndex(p => new { p.LoadId, p.LocationId, p.StopAction }).IsUnique();

it will generates:

CreateIndex("dbo.Stop", new[] { "LoadId", "LocationId", "StopAction" }, unique: true);

Upvotes: 1

Related Questions