Visual studio 2015 如何在Entity Framework 7中为字符串属性创建索引

Visual studio 2015 如何在Entity Framework 7中为字符串属性创建索引,visual-studio-2015,entity-framework-core,Visual Studio 2015,Entity Framework Core,我正在尝试为EntityFramework7创建一个代码优先模型。我正在使用最近发布的Visual Studio 2015 Beta版和以下版本的EntityFramework软件包(来自我的project.json文件的片段): 看起来目前没有可用的数据注释,我正在使用OnModelCreating覆盖和最近实现的(部分?)迁移来创建我的模型 目前,主键和一对一关系与为整数类型创建索引一样有效。例如: builder.Entity<Article>(e => { e.

我正在尝试为EntityFramework7创建一个代码优先模型。我正在使用最近发布的Visual Studio 2015 Beta版和以下版本的EntityFramework软件包(来自我的project.json文件的片段):

看起来目前没有可用的数据注释,我正在使用OnModelCreating覆盖和最近实现的(部分?)迁移来创建我的模型

目前,主键和一对一关系与为整数类型创建索引一样有效。例如:

builder.Entity<Article>(e =>
{
    e.Key(c => c.Id);
    e.OneToOne<Category>(c => c.Category);
    e.Index(c => c.Time).IsUnique(false);
});
但当我尝试向string属性添加索引时,会生成迁移,但当应用时会被SQL Server拒绝,这显然是因为列类型为nvarchar(MAX)。似乎
.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);
        });

有没有办法在EF7的beta版中创建字符串属性索引?

不幸的是,目前(7.0.0-beta1),在确定使用哪种列类型时,不遵守最大长度和列类型元数据。现在,在迁移过程中,您必须直接使用原始DDL

//在CreateIndex之前添加
Sql(“altertable[Keyword]altercolumn[Word]nvarchar(4000)”;

顺便说一句,我也提交了。谢谢你提供的信息。目前,我手动编辑生成的迁移文件,添加了
dataType
参数:
migrationBuilder.CreateTable(“关键字”,c=>new{Id=c.Int(nullable:false,identity:true),Word=c.String(nullable:false,maxLength:100,数据类型:“nvarchar(100)”)}.PrimaryKey(“PK_关键字”,t=>t.Id);
看起来您的变体也应该在生成的迁移文件中执行,而不是在迁移代码中执行onmodel创建重载?正确。(我已经澄清了我的答案。)
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);
        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);