2014-06-21 13 views
15

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 StringPropertyConfigurationPrimitivePropertyConfiguration 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)) 

Trả lời

25

Điều này là do mỗi người trong số các phương pháp mở rộng của bạn gán một chú thích mới cho thuộc tính và ghi đè lên thuộc tính trước đó. Hãy để tôi chỉ ra rằng bằng cách sử dụng các phương pháp của bạn trong một ví dụ.

Nói rằng chúng tôi có điều này (vô dụng) lớp

public class Client 
{ 
    public int ClientId { get; set; } 
    public int CompanyId { get; set; } 
    public int AddressId { get; set; } 
} 

Và áp dụng định nghĩa chỉ số của bạn (bỏ qua phần 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); 

nội tuyến phương pháp khuyến nông (tạ ơn Chúa vì Resharper) dẫn này đến

.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)); 

Điều này cũng giống như viết

[Index("ClientCompanyIndex", Order = 1)] 
public int ClientId { get; set; } 

và sau đó thay thế nó bằng cách

[Index("ClientAddressIndex", Order = 1)] 
public int ClientId { get; set; } 

Để tái tạo các chú thích chính xác ...

[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; } 

... cấu hình của ClientId tài sản nên trông giống như

.Property(c => c.ClientId).HasColumnAnnotation("Index", 
    new IndexAnnotation(new[] 
     { 
      new IndexAttribute("ClientCompanyIndex", 1), 
      new IndexAttribute("ClientAddressIndex", 1) 
     })); 

Bây giờ, đột nhiên tạo các phương thức mở rộng ít hấp dẫn hơn nhiều. Nó hầu như không đáng để tạo một cái cho chú thích kết hợp này. Nhưng đối với các cột sử dụng một lần, các phương pháp của bạn là một cải tiến.

Tất nhiên, rõ ràng tại sao bạn đang thử điều này. Cú pháp thông thạo hiện tại rất khó để nói ít nhất. Nhóm EF knows this perfectly well và họ hy vọng một số người đóng góp sẽ sớm nhận được vấn đề này. Có thể một cái gì đó cho bạn?

+0

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

+0

+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} '. –

Các vấn đề liên quan