我仍然在熟悉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" });
}
这样的劳动果实: