ASP.NET Core Entity Framework Core无法找到IndexAttribute。

17
在我的Mac上,在执行“dotnet run”后,我在Visual Studio Code的IDE和控制台窗口中都收到以下消息: “找不到IndexAttribute类型或命名空间名称”。 我有一个名为Story的类,我想用它来生成一个Code First数据库。这个类有一个标记为KeyAttribute的主键和一个标记为MaxLengthAttribute的Author字符串(使用System.ComponentModel.DataAnnotations)。另外两个字段,DateTime Date和bool IsPublished,应用了IndexAttribute(它是一个由两列组成的索引)。我明确将其命名为IX_DatePublished,IsUnique = false,并为Date字段使用Order = 1,对于IsPublished字段使用Order = 2。
请问: 1. 在运行“dotnet restore”之前,我需要在project.json文件中放置什么以使IndexAttribute正常工作? 2. 是否ASP.NET Core 1 for Mac / Linux附带的EF没有包括正确的命名空间?
谢谢!
3个回答

24

我仍在熟悉核心工具的过程中;进一步的研究表明,该功能不受支持,但他们会考虑一项 pull request。

https://github.com/aspnet/EntityFrameworkCore/issues/4050

解决方法

在缺乏 IndexAttribute 的情况下向 Code First 模型添加索引的推荐方法是使用 Entity Framework Fluent 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

值得注意的是,生成哪种迁移代码来创建此索引很有趣(您可以直接使用此代码自定义迁移以创建索引,而无需将代码添加到上下文类中):

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" });
}
这样的努力结出了成果:

SQLiteStudio显示生成的表格


13

自从您提出这个问题以来,情况似乎已经改变了。jsakamoto实现了NuGet包,允许您保留[Index]属性。唯一的区别是变量的顺序; 您不能再将Order=0作为最后一个变量,而是要这样做:

[Index("IX_TreeFiddy", 0, IsUnique = false)]
public string LochNessMonster { get; set; }

[Index("IX_TreeFiddy", 1, IsUnique = false)]
public int CentsGiven { get; set; }

重写 OnModelCreating()

// Override "OnModelCreating"
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    // .. and invoke "BuildIndexesFromAnnotations"!
    modelBuilder.BuildIndexesFromAnnotations();
}

这里是链接:.NET Core上的IndexAttribute NuGet包


3

现在,EF Core 5.0和标准命名空间原生支持此功能:

using Microsoft.EntityFrameworkCore;

您可以通过类级别的属性定义索引:
[Index(nameof(Date), nameof(Published), IsUnique = false)]
public class Story
{
    public int Id { get; set; }
    public DateTime Date { get; set; }
    public bool Published { get; set; }
}

您可以指定一个列作为索引,或者像上面展示的那样指定多个列创建复合键。更多信息请点击此处


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接