5

Entity Framework 7 のコード ファースト モデルを作成しようとしています。最近リリースされた Visual Studio 2015 Beta と、次のバージョンの EntityFramework パッケージ (私の project.json ファイルのスニペット) を使用しています。

"EntityFramework.SqlServer": "7.0.0-beta1",
"EntityFramework.Commands": "7.0.0-beta1",

現在、データ注釈は利用できないようで、OnModelCreating オーバーライドと最近実装された (部分的に?) 移行を使用してモデルを作成しています。

今のところ、主キーと 1 対 1 の関係は、整数型のインデックスの作成と同様に機能します。例えば:

builder.Entity<Article>(e =>
{
    e.Key(c => c.Id);
    e.OneToOne<Category>(c => c.Category);
    e.Index(c => c.Time).IsUnique(false);
});

このスニペットにより、次の移行コードが生成されます。

migrationBuilder.CreateTable("Article",
            c => new
                {
                    Id = c.String(),
// ...
                    CategoryIdKey = c.Int(nullable: false),
                    Time = c.DateTime(nullable: false),
// ...
               })
            .PrimaryKey("PK_Article", t => t.Id)
            .UniqueConstraint("UC_Article_CategoryIdKey", t => t.CategoryIdKey);

        migrationBuilder.AddForeignKey("Category", "FK_Category_Article_CategoryId", new[] { "CategoryId" }, "Article", new[] { "CategoryIdKey" }, cascadeDelete: false);

        migrationBuilder.CreateIndex("Article", "IX_Article_Time", new[] { "Time" }, isUnique: false, isClustered: false);

しかし、文字列プロパティにインデックスを追加しようとすると、移行が生成されますが、列の型が nvarchar(MAX) であるため、適用すると SQL Server によって拒否されます。.Required().MaxLength(100)制限された文字列列タイプの生成を強制しないようです。列の型を変更する方法はありますが、ModelBuilder から呼び出す方法が見つからないようです。

        builder.Entity<Keyword>(e =>
        {
            e.Key(c => c.Id);
            e.Property(c => c.Word).Required().MaxLength(100);
            e.Index(c => c.Word).IsUnique(true);
        });

移行の結果:

        migrationBuilder.CreateTable("Keyword",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Word = c.String(nullable: false, maxLength: 100)
                })
            .PrimaryKey("PK_Keyword", t => t.Id);

        migrationBuilder.CreateIndex("Keyword", "IX_Keyword_Word", new[] { "Word" }, isUnique: true, isClustered: false);

EF7 のベータ版で文字列プロパティにインデックスを作成する方法はありますか?

4

1 に答える 1