How to create index in Entity Framework 6.2 with code first -
is there way create index on property/column using code-first, instead of using new indexattribute
?
currently there no "first class support" creating index via fluent api, can via fluent api can mark properties having attributes annotation api. allow add index
attribute via fluent interface.
here examples work item issues site ef.
create index on single column:
modelbuilder.entity<myentity>() .property(e => e.myproperty) .hascolumnannotation( indexannotation.annotationname, new indexannotation(new indexattribute()));
multiple indexes on single column:
modelbuilder.entity<myentity>() .property(e => e.myproperty) .hascolumnannotation( indexannotation.annotationname, new indexannotation(new[] { new indexattribute("index1"), new indexattribute("index2") { isunique = true } }));
multi-column indexes:
modelbuilder.entity<myentity>() .property(e => e.myproperty1) .hascolumnannotation( indexannotation.annotationname, new indexannotation(new indexattribute("myindex", 1))); modelbuilder.entity<myentity>() .property(e => e.myproperty2) .hascolumnannotation( indexannotation.annotationname, new indexannotation(new indexattribute("myindex", 2)));
using above techniques cause .createindex()
calls automatically created in up()
function when scaffold next migration (or automatically created in database if not using migrations).
Comments
Post a Comment