6
私は「DOTNETの実行」の両方のIDEにし、コンソールウィンドウで実行した後、私のMac上のVisual Studioのコードから、次の受信

を見つけることができません見つからないAsp.netコアEntity FrameworkのはIndexAttribute

私はコードファーストでデータベースを生成するために使用したいStoryというクラスがあります。このクラスには、KeyAttributeとMaxLengthAttributeでマークされた作成者の文字列が表示されているプラ​​イマリキーがあるため、両方とも動作します(System.ComponentModel.DataAnnotationsを使用)。 DateTime Dateとbool IsPublishedの2つのフィールドに、IndexAttributeが適用されています(これは2列のインデックスです)。明示的に名前をIX_DatePublished、IsUnique = falseとし、DateフィールドにOrder = 1、IsPublishedフィールドにOrder = 2を使用します。

  • "dotnet restore"を実行してIndexAttributeを動作させるにはどうすればよいですか?
  • Mac/Linux用のASPCore1に含まれるEFには正しい名前空間が含まれていませんか?

ありがとうございました!

答えて

9

私はまだコアツールに精通しています。さらに調査したところ、この機能はサポートされていませんが、プルリクエストを検討することになりました。

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

IndexAttributeが存在しない場合に、コードファーストモデルにインデックスを追加するための回避策

推奨される方法は、Entity Frameworkの流暢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用の2つのカラムのインデックスを作成し、それがユニークではありません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" }); 
} 

、このような労働者の果物:

SQLiteStudio showing the generated table

関連する問題