Wygląda na to, że w Entity Framework 6.1 dodano możliwość tworzenia indeksów tabel za pomocą nowej metody HasColumnAnnotation
. Stworzyłem kilka rozszerzeń pomocnika, aby przyspieszyć proces:Wiele indeksów możliwe przy użyciu 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 }));
}
}
Działa to fantastyczne ... aż próbuję utworzyć drugi indeks, który zawiera kolumnę już używany w innym indeksem. Wszystko, co dodaję, powoduje nadpisanie oryginału. Czy ktoś wie, czy obecnie możliwe jest dodawanie wielu indeksów do tej samej kolumny za pośrednictwem nowego HasColumnAnnotation
dostępnego na StringPropertyConfiguration
i PrimitivePropertyConfiguration
?
Mogę to obejść tak, jak zawsze, ręcznie dodając indeksy w skryptach migracji, ale najlepiej byłoby móc skonfigurować to w mapowaniach EntityTypeConfiguration
, więc mogę mieć wszystko w jednym miejscu.
Po Gerts sprzężenia zwrotnego, to jest to, co skończyło się robi:
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));
}
}
A oto nowy zwyczaj:
Property(x => x.Name).IsRequired().HasMaxLength(65).HasIndex(new IndexAttribute("IX_Countries_Name") { IsUnique = true }, new IndexAttribute("IX_Countries_Published", 2))
Dziękuję Gert. Pomyślałem, że to może być to, co się dzieje. Ostatni przykład pokazuje mi, co muszę zrobić dalej.Prawdopodobnie dostosuję moją klasę pomocniczą, aby zaakceptować 'parametry IndexAttribute [] index', dzięki czemu mogę przekazywać wiele indeksów dla każdej właściwości. Nowy kod dodany do pytania. – Sam
+1. W twoim ostatnim fragmencie przykładowego kodu przegapiłeś atrybut 'IsUnique', powinien to być' nowy IndexAttribute ("ClientCompanyIndex", 1) {IsUnique = true} 'i' new IndexAttribute ("ClientAddressIndex", 1) {IsUnique = true} '. –