visual studio 2015 - How to create an index for a string property in Entity Framework 7 -
i'm trying create code first model entity framework 7. i'm using released visual studio 2015 beta , following versions of entityframework packages (snippet project.json file):
"entityframework.sqlserver": "7.0.0-beta1", "entityframework.commands": "7.0.0-beta1",
it looks no data annotations available , i'm using onmodelcreating override , implemented (partly?) migrations create model.
for now, primary keys , one-to-one relations work creating indices integer types. example:
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 following migrations code generated:
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'm trying add index string property, migration generated, when applied rejected sql server, apparently due column type being nvarchar(max). seems .required().maxlength(100)
not force limited string column type generation. , though there method change column type, cannot seem find way call 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); });
resulting 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 way create index on string property in beta version of ef7?
unfortunately @ time (7.0.0-beta1), max length , column type metadata not honored when determining column type use. now, you'll have drop down raw ddl in migration.
// add before createindex migrationbuilder.sql("alter table [keyword] alter column [word] nvarchar(4000)");
Comments
Post a Comment