I am trying to create the first code model for Entity Framework 7. I am using the recently released version of Visual Studio 2015 Beta and the following versions of EntityFramework packages (snippet from project.json file):
"EntityFramework.SqlServer": "7.0.0-beta1", "EntityFramework.Commands": "7.0.0-beta1",
It looks like there are currently no data annotations, and I'm using the OnModelCreating override and recently implemented (partially?) Migrations to create my model.
Primary keys and one-to-one relationships, as well as indexes for integer types, currently work. For instance:
builder.Entity<Article>(e => { e.Key(c => c.Id); e.OneToOne<Category>(c => c.Category); e.Index(c => c.Time).IsUnique(false); });
This snippet results in the following migration code:
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);
But when I try to add the index property to the row, the migration is generated, but when using SQL Server it is rejected, apparently because the column type is nvarchar (MAX). It seems that .Required().MaxLength(100) does not generate a restricted row column. And although there is a way to change the type of a column, I cannot find a way to call it through 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); });
Effective migration:
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);
Is there a way to create an index for the string property in the EF7 beta?