私はまだコアツールに精通しています。さらに調査したところ、この機能はサポートされていませんが、プルリクエストを検討することになりました。
https://github.com/aspnet/EntityFrameworkCore/issues/4050
IndexAttributeが存在しない場合に、コードファーストモデルにインデックスを追加するための回避策
推奨される方法は、Entity Frameworkの流暢APIを使用することです。たとえば、次は(DbContext由来)あなたのコンテキストに追加することができます
/// <summary>
/// Creates database structure not supported with Annotations using Fluent syntax.
/// </summary>
/// <param name="optionsBuilder">The configuration interface.</param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Story>().HasIndex(
story => new { story.Date, story.Published }).IsUnique(false);
}
これはStory.Date用の2つのカラムのインデックスを作成し、それがユニークではありませんStory.Published。この変更に伴い、使用:
dotnet ef migrations add <name>
dotnet ef database update
それはこのインデックスを作成するために生成される移行コードの種類に注意することは興味深いです(あなたの代わりにあなたのContextクラスにコードを追加するのでインデックスを作成するために、直接あなたの移行をカスタマイズするためにこれを使用することができます) :
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Stories",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Autoincrement", true),
Author = table.Column<string>(maxLength: 64, nullable: true),
Date = table.Column<DateTime>(nullable: false),
Published = table.Column<bool>(nullable: false),
Title = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Stories", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_Stories_Date_Published",
table: "Stories",
columns: new[] { "Date", "Published" });
}
、このような労働者の果物: