C# 重构模式定义中的许多HasColumnAnnotation和IndexAnnotation用法

C# 重构模式定义中的许多HasColumnAnnotation和IndexAnnotation用法,c#,entity-framework,lambda,C#,Entity Framework,Lambda,我有许多代码块,如下所示: modelBuilder .Entity<Foo>() .Property(t => t.X) .IsRequired() .HasMaxLength(60) .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_X_Y", 1) { IsUnique = true }));

我有许多代码块,如下所示:

modelBuilder
    .Entity<Foo>()
    .Property(t => t.X)
    .IsRequired()
    .HasMaxLength(60)
    .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_X_Y", 1) { IsUnique = true }));

modelBuilder
    .Entity<Foo>()
    .Property(t => t.Y)
    .IsRequired()
    .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_X_Y", 2) { IsUnique = true }));
我想重构它,使它看起来像:

CreateCompositeUnique<Foo>(modelBuilder, "IX_X_Y", t => new {t.X, t.Y});
CreateCompositeUnique<Bar>(modelBuilder, "IX_R_S", t => new {t.R, t.S});
CreateCompositeUnique(modelBuilder,“IX_X_Y”,t=>new{t.X,t.Y});
CreateCompositeUnique(modelBuilder,“IX_R_S”,t=>new{t.R,t.S});
我在想这样的事情:

private void CreateCompositeUnique<T>(DbModelBuilder modelBuilder, string indexName, List<Expression<Func<T, byte[]>>> properties)
{ 
    for (int i = 0; i < properties.Count; i++)
    {
        modelBuilder
        .Entity<typeof(T)>()
        .Property(properties[i])
        .IsRequired() 
        .HasMaxLength(60)  // --only when propery is string
        .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute(indexName, i) { IsUnique = true }));
    }
}
modelBuilder
    .Entity<Foo>()
    .Property(t => t.X)
    .IsRequired()
    .HasMaxLength(60)
    .HasIndex("IX_X_Y", 1); // <-- here (X is string)

modelBuilder
    .Entity<Foo>()
    .Property(t => t.Y)
    .IsRequired()
    .HasIndex("IX_X_Y", 2); // <-- and here (Y is a primitive)
private void CreateCompositeUnique(DbModelBuilder modelBuilder,字符串索引名,列表属性)
{ 
对于(int i=0;i
但我有一些问题:

  • 这是个好主意吗
  • 如何知道属性是否为字符串
  • 传递参数的最佳方式是什么
  • 如何访问“T”?我在
    .Entity

  • 按照Gert Arnold的建议,我创建了一个扩展方法。实际上,它们是两种扩展方法(为什么?自动解释,请参见代码注释)

    用法如下:

    private void CreateCompositeUnique<T>(DbModelBuilder modelBuilder, string indexName, List<Expression<Func<T, byte[]>>> properties)
    { 
        for (int i = 0; i < properties.Count; i++)
        {
            modelBuilder
            .Entity<typeof(T)>()
            .Property(properties[i])
            .IsRequired() 
            .HasMaxLength(60)  // --only when propery is string
            .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute(indexName, i) { IsUnique = true }));
        }
    }
    
    modelBuilder
        .Entity<Foo>()
        .Property(t => t.X)
        .IsRequired()
        .HasMaxLength(60)
        .HasIndex("IX_X_Y", 1); // <-- here (X is string)
    
    modelBuilder
        .Entity<Foo>()
        .Property(t => t.Y)
        .IsRequired()
        .HasIndex("IX_X_Y", 2); // <-- and here (Y is a primitive)
    
    modelBuilder
    .实体()
    .Property(t=>t.X)
    .IsRequired()
    .HasMaxLength(60)
    .HasIndex(“IX_X_Y”,1);//t、 Y)
    .IsRequired()
    
    .HasIndex(“IX_X_Y”,2);//我不会费心这么做的。您可能会发现自己在这个方法中添加了越来越多的参数,以解释类型之间的细微差异。由于配置是代码库中最稳定的部分(通常),我不介意某种程度的重复。您可以使用一个简单的语法来简化这个笨拙的
    hascollannotation
    语法。而且,我不喜欢一个比它的名字所暗示的做得更多的方法
    CreateCompositeUnique
    不应该设置像max length这样的约束。我喜欢扩展方法的想法