Mehrere Indizes möglich mit HasColumnAnnotation?

Sieht es aus wie in Entity Framework 6.1 Sie die Möglichkeit Hinzugefügt, um die create table-Indizes über die neue HasColumnAnnotation Methode. Erstellt habe ich ein paar Helfer Erweiterungen um den Prozess zu beschleunigen:

public static class MappingExtensions
{
    public static StringPropertyConfiguration HasIndex(this StringPropertyConfiguration config, bool isUnique = false)
    {
        return config.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute() { IsUnique = isUnique }));
    }
    public static StringPropertyConfiguration HasIndex(this StringPropertyConfiguration config, string name, int order = 1, bool isUnique = false)
    {
        return config.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute(name, order) { IsUnique = isUnique }));
    }
    public static PrimitivePropertyConfiguration HasIndex(this PrimitivePropertyConfiguration config, bool isUnique = false)
    {
        return config.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute() { IsUnique = isUnique }));
    }
    public static PrimitivePropertyConfiguration HasIndex(this PrimitivePropertyConfiguration config, string name, int order = 1, bool isUnique = false)
    {
        return config.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute(name, order) { IsUnique = isUnique }));
    }
}

Dies funktioniert fantastisch...bis ich versuchen, zu erstellen ein zweiter index enthält eine Spalte, die bereits in einem anderen index. Was auch immer ich als letztes fügen überschreibt das original. Weiß jemand, ob es derzeit möglich, mehrere Indizes in der gleichen Spalte über die neue HasColumnAnnotation erhältlich auf der StringPropertyConfiguration und PrimitivePropertyConfiguration?

Kann ich umgehen, wie ich es immer haben durch manuelles hinzufügen von Indizes in der Migration-Skripte, aber es würde die meisten ausgezeichnete zu können, konfigurieren Sie diese in der EntityTypeConfiguration Zuordnungen, so kann ich Sie alle in einem Punkt.


Nach Gerts feedback, das ist, was ich am Ende machen:

public static class MappingExtensions
{
    public static StringPropertyConfiguration HasIndex(this StringPropertyConfiguration config, params IndexAttribute[] indexes)
    {
        return config.HasColumnAnnotation("Index", new IndexAnnotation(indexes));
    }

    public static PrimitivePropertyConfiguration HasIndex(this PrimitivePropertyConfiguration config, params IndexAttribute[] indexes)
    {
        return config.HasColumnAnnotation("Index", new IndexAnnotation(indexes));
    }
}

Und hier ist die neue Verwendung:

Property(x => x.Name).IsRequired().HasMaxLength(65).HasIndex(new IndexAttribute("IX_Countries_Name") { IsUnique = true }, new IndexAttribute("IX_Countries_Published", 2))
InformationsquelleAutor Sam | 2014-06-21
Schreibe einen Kommentar