c# - Multiple indexes possible using HasColumnAnnotation? -
it looks in entity framework 6.1 added ability create table indexes via new hascolumnannotation
method. created few helper extensions speed process:
public static class mappingextensions { public static stringpropertyconfiguration hasindex(this stringpropertyconfiguration config, bool isunique = false) { homecoming config.hascolumnannotation("index", new indexannotation(new indexattribute() { isunique = isunique })); } public static stringpropertyconfiguration hasindex(this stringpropertyconfiguration config, string name, int order = 1, bool isunique = false) { homecoming config.hascolumnannotation("index", new indexannotation(new indexattribute(name, order) { isunique = isunique })); } public static primitivepropertyconfiguration hasindex(this primitivepropertyconfiguration config, bool isunique = false) { homecoming config.hascolumnannotation("index", new indexannotation(new indexattribute() { isunique = isunique })); } public static primitivepropertyconfiguration hasindex(this primitivepropertyconfiguration config, string name, int order = 1, bool isunique = false) { homecoming config.hascolumnannotation("index", new indexannotation(new indexattribute(name, order) { isunique = isunique })); } }
this works fantastic...until seek create sec index contains column used in index. whatever add together lastly overwrites original. know if possible add together multiple indexes same column via new hascolumnannotation
available on stringpropertyconfiguration
, primitivepropertyconfiguration
?
i can work around have manually adding indexes in migration scripts, first-class able configure in entitytypeconfiguration
mappings can have in 1 spot.
after gerts feedback, ended doing:
public static class mappingextensions { public static stringpropertyconfiguration hasindex(this stringpropertyconfiguration config, params indexattribute[] indexes) { homecoming config.hascolumnannotation("index", new indexannotation(indexes)); } public static primitivepropertyconfiguration hasindex(this primitivepropertyconfiguration config, params indexattribute[] indexes) { homecoming config.hascolumnannotation("index", new indexannotation(indexes)); } }
and here new usage:
property(x => x.name).isrequired().hasmaxlength(65).hasindex(new indexattribute("ix_countries_name") { isunique = true }, new indexattribute("ix_countries_published", 2))
this because each of extension methods assign new annotation property , overwrite previous one. allow me show using methods in example.
say have (useless) class
public class client { public int clientid { get; set; } public int companyid { get; set; } public int addressid { get; set; } }
and apply index definitions (skipping part modelbuilder.entity<client>()
):
.property(c => c.clientid).hasindex("clientcompanyindex"); .property(c => c.companyid).hasindex("clientcompanyindex", 2); .property(c => c.clientid).hasindex("clientaddressindex"); .property(c => c.addressid).hasindex("clientaddressindex", 2);
inlining extension methods (thank god resharper) leads to
.property(c => c.clientid).hascolumnannotation("index", new indexannotation(new indexattribute("clientcompanyindex", 1)); .property(c => c.companyid).hascolumnannotation("index", new indexannotation(new indexattribute("clientcompanyindex", 2)); .property(c => c.clientid).hascolumnannotation("index", new indexannotation(new indexattribute("clientaddressindex", 1)); .property(c => c.addressid).hascolumnannotation("index", new indexannotation(new indexattribute("clientaddressindex", 2));
this same writing
[index("clientcompanyindex", order = 1)] public int clientid { get; set; }
and replacing by
[index("clientaddressindex", order = 1)] public int clientid { get; set; }
to reproduce right annotation...
[index("clientaddressindex", isunique = true, order = 1)] [index("clientcompanyindex", isunique = true, order = 1)] public int clientid { get; set; } [index("clientcompanyindex", isunique = true, order = 2)] public int companyid { get; set; } [index("clientaddressindex", isunique = true, order = 2)] public int addressid { get; set; }
...the configuration of clientid
property should like
.property(c => c.clientid).hascolumnannotation("index", new indexannotation(new[] { new indexattribute("clientcompanyindex", 1), new indexattribute("clientaddressindex", 1) }));
now creating extension methods far less appealing. it's hardly worth effort create 1 combined annotation. single-use columns methods improvement.
of course of study clear why you're trying this. current fluent syntax clunky least. ef team knows well , they're hoping contributor grab issue soon. maybe you?
c# entity-framework ef-code-first ef-fluent-api
No comments:
Post a Comment