IndexingPropertyConvention.cs 879 B

1234567891011121314151617181920212223
  1. using System;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. using System.Data.Entity.Infrastructure.Annotations;
  4. using System.Data.Entity.ModelConfiguration.Conventions;
  5. using System.Linq.Expressions;
  6. namespace IwbZero.EntityFramework
  7. {
  8. public class IndexingPropertyConvention<T, TProperty> : Convention where T : class
  9. {
  10. public IndexingPropertyConvention(Expression<Func<T, TProperty>> propertyExpression, Func<Type, bool> predicate = null)
  11. {
  12. predicate = predicate ?? (t => true);
  13. Types<T>()
  14. .Where(t => predicate(t))
  15. .Configure(e => e.Property(propertyExpression).HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute
  16. {
  17. IsClustered = false,
  18. IsUnique = false
  19. })));
  20. }
  21. }
  22. }