EF Fluent API - 复杂字段的 HasIndex

EF Fluent API - HasIndex for complex field

我需要使用 Fluent API(没有 DataAnnotation)并且应该创建复杂的索引(3 个字段)。我尝试这样做:

        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();

但上面写着:

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.

怎么做?

我找到了解决方案:

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

它将生成:

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