C# Asp.net核心实体框架找不到IndexAttribute

C# Asp.net核心实体框架找不到IndexAttribute,c#,asp.net,entity-framework,asp.net-core,asp.net-core-1.0,C#,Asp.net,Entity Framework,Asp.net Core,Asp.net Core 1.0,在执行“dotnet run”后,我在IDE和控制台窗口中从Mac上的Visual Studio代码接收到以下内容: 找不到类型或命名空间名称“IndexAttribute” 我有一个名为Story的类,我想先用它生成一个带有代码的数据库。这个类有一个用KeyAttribute标记的主键和一个用MaxLengthAttribute标记的作者字符串,所以这两个都可以工作(使用System.ComponentModel.DataAnnotations)。另外两个字段DateTime Date和boo

在执行“dotnet run”后,我在IDE和控制台窗口中从Mac上的Visual Studio代码接收到以下内容:

找不到类型或命名空间名称“IndexAttribute”

我有一个名为Story的类,我想先用它生成一个带有代码的数据库。这个类有一个用KeyAttribute标记的主键和一个用MaxLengthAttribute标记的作者字符串,所以这两个都可以工作(使用System.ComponentModel.DataAnnotations)。另外两个字段DateTime Date和bool IsPublished应用了IndexAttribute(这是一个两列索引)。我明确地将其命名为IX_DatePublished,IsUnique=false,并将Order=1用于日期字段,将Order=2用于IsPublished字段

  • 在运行“dotnet restore”之前,我应该在project.json中添加什么内容,以便它能够为IndexAttribute提供正确的功能
  • ASPCore1 for Mac/Linux附带的EF是否包含正确的命名空间

谢谢大家!

我仍在熟悉核心工具的过程中;进一步的研究表明,这个功能不被支持,但是他们会考虑拉请求。

解决方案

在没有IndexAttribute的情况下,向代码优先模型添加索引的推荐方法是使用实体框架Fluent API。例如,可以将以下内容添加到您的上下文(从DbContext派生):

//
///使用流利语法创建批注不支持的数据库结构。
/// 
///配置界面。
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity().HasIndex(
story=>new{story.Date,story.Published}).IsUnique(false);
}
这将为Story.Date和Story.Published创建一个两列索引,这不是唯一的。进行此更改后,请使用:

dotnet ef migrations add <name>
dotnet ef database update
dotnet ef迁移添加
dotnet ef数据库更新
有趣的是,要注意生成了什么样的迁移代码来创建这个索引(您可以直接使用它来定制迁移以创建索引,而不是将代码添加到上下文类中):

protected override void Up(MigrationBuilder MigrationBuilder)
{
migrationBuilder.CreateTable(
名称:“故事”,
列:表=>new
{
Id=table.Column(可空:false)
.注释(“自动增量”,真),
Author=table.Column(maxLength:64,nullable:true),
日期=表.列(可空:false),
Published=table.Column(可空:false),
Title=table.Column(可空:true)
},
约束:表=>
{
表.PrimaryKey(“PK_故事”,x=>x.Id);
});
migrationBuilder.CreateIndex(
名称:“九篇故事发布日期”,
表:“故事”,
列:新[]{“日期”,“发布”});
}
这些劳动的成果:


自从你提出这个问题以来,情况似乎有所改变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; }
重写模型创建()

以下是链接:

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" });
}
[Index("IX_TreeFiddy", 0, IsUnique = false)]
public string LochNessMonster { get; set; }

[Index("IX_TreeFiddy", 1, IsUnique = false)]
public int CentsGiven { get; set; }
// Override "OnModelCreating"
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

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