我仍然在熟悉Core工具;進一步的研究表明,這個功能不被支持,但他們會考慮拉請求。
https://github.com/aspnet/EntityFrameworkCore/issues/4050
的變通
推薦的方法添加索引到的Code First模型在沒有IndexAttribute的是使用實體框架流利的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兩列索引和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" });
}
這樣的勞動果實: