I receive the following from Visual Studio Code on my Mac, both in IDE and console window after executing "dotnet run":
在执行“dotnet run”后,我在Mac上的Visual Studio Code中收到以下内容,包括IDE和控制台窗口:
The type or namespace name 'IndexAttribute' could not be found
找不到类型或命名空间名称“IndexAttribute”
I have a class called Story which I want to use for generating a database with Code First. This class has a primary key marked with KeyAttribute and Author string marked with MaxLengthAttribute, so both of those work (using System.ComponentModel.DataAnnotations). Two more fields, DateTime Date and bool IsPublished, have IndexAttribute applied (it's a two-column index). I explicitly named it IX_DatePublished, IsUnique = false, and use Order = 1 for the Date field and Order = 2 for the IsPublished field.
我有一个名为Story的类,我想用它来生成Code First数据库。此类有一个标记有KeyAttribute的主键和使用MaxLengthAttribute标记的Author字符串,因此这两个都有效(使用System.ComponentModel.DataAnnotations)。另外两个字段,DateTime Date和bool IsPublished,应用了IndexAttribute(它是一个双列索引)。我明确地将其命名为IX_DatePublished,IsUnique = false,并将Date = 1用于Date字段,将Order = 2用于IsPublished字段。
- What do I put in project.json before running "dotnet restore" to have it pull in the right stuff for IndexAttribute to work?
- 在运行“dotnet restore”之前我应该在project.json中添加什么来让它为IndexAttribute提供正确的工作?
- Does EF included with ASPCore1 for Mac/Linux not have the right namespace included?
- 包含在Mac / Linux的ASPCore1中的EF是否包含正确的命名空间?
Thank you!
谢谢!
2 个解决方案
#1
2
This seems to have changed since you asked the question. jsakamoto has implemented NuGet package that allows you to keep your [Index] attribute. The only difference is order of variables; you can no longer have Order=0
as your last variable but rather do:
自从您提出问题以来,这似乎已经发生了变化。 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; }
Here is link to: IndexAttribute for .NET Core NuGet package
以下是指向.NET Core NuGet包的IndexAttribute的链接
#2
14
I am still in the process of getting familiar with Core tools; further research revealed that this feature is not supported but they would consider a pull request.
我仍然在熟悉核心工具;进一步的研究表明,不支持此功能,但他们会考虑拉取请求。
https://github.com/aspnet/EntityFrameworkCore/issues/4050
https://github.com/aspnet/EntityFrameworkCore/issues/4050
The work-around
解决方法
The recommended way to add indexes to Code First models in the absence of IndexAttribute is to use Entity Framework Fluent API. For example the following could be added to your context (derived from DbContext):
在没有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);
}
This creates a two-column index for Story.Date and Story.Published that's not unique. Following this change, use:
这为Story.Date和Story.Published创建了一个双列索引,它不是唯一的。完成此更改后,请使用:
dotnet ef migrations add <name>
dotnet ef database update
It's interesting to note what kind of Migration code is generated to create this index (you could use this directly to customize your migrations to create index instead of adding code to your Context class):
有趣的是要注意生成哪种迁移代码来创建此索引(您可以直接使用它来自定义迁移以创建索引而不是向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" });
}
The fruit of such labors:
这些劳动的成果:
#1
2
This seems to have changed since you asked the question. jsakamoto has implemented NuGet package that allows you to keep your [Index] attribute. The only difference is order of variables; you can no longer have Order=0
as your last variable but rather do:
自从您提出问题以来,这似乎已经发生了变化。 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; }
Here is link to: IndexAttribute for .NET Core NuGet package
以下是指向.NET Core NuGet包的IndexAttribute的链接
#2
14
I am still in the process of getting familiar with Core tools; further research revealed that this feature is not supported but they would consider a pull request.
我仍然在熟悉核心工具;进一步的研究表明,不支持此功能,但他们会考虑拉取请求。
https://github.com/aspnet/EntityFrameworkCore/issues/4050
https://github.com/aspnet/EntityFrameworkCore/issues/4050
The work-around
解决方法
The recommended way to add indexes to Code First models in the absence of IndexAttribute is to use Entity Framework Fluent API. For example the following could be added to your context (derived from DbContext):
在没有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);
}
This creates a two-column index for Story.Date and Story.Published that's not unique. Following this change, use:
这为Story.Date和Story.Published创建了一个双列索引,它不是唯一的。完成此更改后,请使用:
dotnet ef migrations add <name>
dotnet ef database update
It's interesting to note what kind of Migration code is generated to create this index (you could use this directly to customize your migrations to create index instead of adding code to your Context class):
有趣的是要注意生成哪种迁移代码来创建此索引(您可以直接使用它来自定义迁移以创建索引而不是向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" });
}
The fruit of such labors:
这些劳动的成果: