c# - ASP.NET Core Entity Framework Core 找不到 IndexAttribute

标签 c# asp.net-core entity-framework-core asp.net-core-1.0

在执行“dotnet run”后,我在我的 Mac 上的 IDE 和控制台窗口中收到来自 Visual Studio Code 的以下信息:

The type or namespace name 'IndexAttribute' could not be found

我有一个名为 Story 的类,我想使用它通过 Code First 生成数据库。此类有一个标有 KeyAttribute 的主键和标有 MaxLengthAttribute 的 Author 字符串,因此它们都有效(使用 System.ComponentModel.DataAnnotations)。另外两个字段 DateTime Date 和 bool IsPublished 应用了 IndexAttribute(这是一个两列索引)。我明确地将其命名为 IX_DatePublished,IsUnique = false,并将 Order = 1 用于 Date 字段,Order = 2 用于 IsPublished 字段。

  • 在运行“dotnet restore”之前,我应该在 project.json 中添加什么,以使其为 IndexAttribute 工作引入正确的内容?
  • ASPCore1 for Mac/Linux 中包含的 EF 是否没有包含正确的命名空间?

谢谢!

最佳答案

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

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 showing the generated table

关于c# - ASP.NET Core Entity Framework Core 找不到 IndexAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38531092/

相关文章:

c# - 使用 EF Core 查询中的方法

c# - Threadpool is getting used by windows service 问题

c# - 如何跟踪最后一分钟的最大/最小值?

javascript - 是否可以在 MVC Controller 返回中调用模态弹出窗口(javascript)

asp.net-core - Kestrel UseHttps() 方法签名在 .net core 2 中更改

angular - 带有 plesk : Couldn't find a valid certificate with subject 'CN=SigningCertificate' 的身份服务器

visual-studio - VS 解决方案资源管理器分析器感叹号 : ef1000 "possible sql injection vulnerability"

c# - html编码空间的HtmlDecode不是空间

c# - 如何覆盖 TextBox 文本属性

c# - 使用 Linq 在 View 中显示字段名称