Es sieht so aus, als ob in Entity Framework 6.1 die Möglichkeit hinzugefügt wurde, Tabellenindizes über die neue Methode HasColumnAnnotation
zu erstellen. Ich habe ein paar Helfer Erweiterungen um den Prozess zu beschleunigen:Mehrere Indizes möglich mit HasColumnAnnotation?
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 }));
}
}
Dieses fantastische funktioniert ... bis ich versuche, einen zweiten Index zu erstellen, die eine Spalte bereits in einem anderen Index verwendet wird, enthält. Was immer ich hinzufüge, überschreibt das Original. Weiß jemand, ob es derzeit möglich ist, der gleichen Spalte mehrere Indizes über die neue HasColumnAnnotation
hinzuzufügen, die auf der StringPropertyConfiguration
und PrimitivePropertyConfiguration
verfügbar ist?
Ich kann das umgehen, wie ich es immer durch manuelle Hinzufügen von Indexen in den Migrationsskripten habe, aber es wäre am besten, dies in den EntityTypeConfiguration
Zuordnungen zu konfigurieren, damit ich alles an einem Ort haben kann.
Nach Gerts Feedback, das ist, was ich tun endete:
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 Nutzung:
Property(x => x.Name).IsRequired().HasMaxLength(65).HasIndex(new IndexAttribute("IX_Countries_Name") { IsUnique = true }, new IndexAttribute("IX_Countries_Published", 2))
Danke Gert. Ich dachte mir, dass das vielleicht passieren könnte. Das letzte Beispiel zeigt mir, was ich als nächstes machen muss.Ich werde wahrscheinlich meine Hilfsklasse anpassen, um 'params IndexAttribute [] indexes 'zu akzeptieren, so dass ich mehrere Indizes pro Eigenschaft übergeben kann. Neuer Code zur Frage hinzugefügt. – Sam
+1. In Ihrem letzten Beispielcode haben Sie das 'IsUnique' Attribut verpasst, es sollte' neues IndexAttribute ("ClientCompanyIndex", 1) {IsUnique = true} 'und' neues IndexAttribute ("ClientAddressIndex", 1) {IsUnique = sein wahr} '. –