Asp.net核心实体框架找不到IndexAttribute
我收到从Visual Studio代码下面我的Mac上,无论是在IDE和执行后控制台窗口“的dotnet运行”:Asp.net核心实体框架找不到IndexAttribute
类型或命名空间名称“IndexAttribute”可能找不到
我有一个叫做Story的类,我想用它来用Code First生成一个数据库。这个类有一个标有KeyAttribute和Author字符串的主键标记为MaxLengthAttribute,所以这两个工作(使用System.ComponentModel.DataAnnotations)。另外两个字段DateTime Date和bool IsPublished应用了IndexAttribute(这是一个两列索引)。我明确地命名为IX_DatePublished,IsUnique = false,对于Date字段使用Order = 1,对IsPublished字段使用Order = 2。
- 在运行“dotnet restore”之前,我在project.json中放入了什么东西,让它能够为正在使用的索引属性工作?
- 包含在Mac/Linux的ASPCore1中的EF没有包含正确的命名空间吗?
谢谢!
我仍然在熟悉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" });
}
这样的劳动果实: