Dường như trong Entity Framework 6.1, họ thêm khả năng tạo chỉ mục bảng thông qua phương thức HasColumnAnnotation
mới. Tôi đã tạo một vài tiện ích trợ giúp để tăng tốc quá trình:Nhiều chỉ mục có thể sử dụng 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 }));
}
}
Điều này thật tuyệt vời ... cho đến khi tôi cố gắng tạo chỉ mục thứ hai chứa cột đã được sử dụng trong một chỉ mục khác. Bất cứ điều gì tôi thêm lần cuối sẽ ghi đè lên bản gốc. Có ai biết liệu hiện tại có thể thêm nhiều chỉ mục vào cùng một cột qua HasColumnAnnotation
mới có sẵn trên StringPropertyConfiguration
và PrimitivePropertyConfiguration
không?
Tôi có thể làm việc xung quanh điều này như tôi luôn có bằng cách thêm chỉ mục theo cách thủ công trong kịch bản di chuyển, nhưng sẽ tuyệt vời nhất để có thể định cấu hình điều này trong ánh xạ EntityTypeConfiguration
để tôi có thể có tất cả tại một điểm.
Sau phản hồi Gerts, đây là những gì tôi đã kết thúc thực hiện:
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));
}
}
Và đây là cách sử dụng mới:
Property(x => x.Name).IsRequired().HasMaxLength(65).HasIndex(new IndexAttribute("IX_Countries_Name") { IsUnique = true }, new IndexAttribute("IX_Countries_Published", 2))
Cảm ơn bạn Gert. Tôi nghĩ rằng có thể đó là những gì đang xảy ra. Ví dụ cuối cùng cho tôi thấy những gì tôi cần làm tiếp theo.Tôi có thể sẽ điều chỉnh lớp helper của tôi để chấp nhận 'params IndexAttribute [] indexes' để tôi có thể truyền vào nhiều chỉ mục cho mỗi thuộc tính. Đã thêm mã mới vào câu hỏi. – Sam
+1. Trong đoạn mã mẫu cuối cùng của bạn, bạn đã bỏ qua thuộc tính 'IsUnique', nó phải là' new IndexAttribute ("ClientCompanyIndex", 1) {IsUnique = true} 'và' new IndexAttribute ("ClientAddressIndex", 1) {IsUnique = true} '. –