首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重构我的架构定义中的许多HasColumnAnnotation和IndexAnnotation用法

重构我的架构定义中的许多HasColumnAnnotation和IndexAnnotation用法
EN

Stack Overflow用户
提问于 2014-10-09 16:31:33
回答 1查看 481关注 0票数 1

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

代码语言:javascript
复制
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 }));

这个块告诉EF,通过fluent API,在表XY的表Foo上创建一个具有列FooY的唯一索引。

另一个类似于此的代码块是这样的,表Bar上有列SBar

代码语言:javascript
复制
modelBuilder
    .Entity<Bar>()
    .Property(t => t.R)
    .IsRequired()
    .HasMaxLength(60)
    .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_R_S", 1) { IsUnique = true }));

modelBuilder
    .Entity<Bar>()
    .Property(t => t.S)
    .IsRequired()
    .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_R_S", 2) { IsUnique = true }));

我想重构它,这样它最终看起来就像:

代码语言:javascript
复制
CreateCompositeUnique<Foo>(modelBuilder, "IX_X_Y", t => new {t.X, t.Y});
CreateCompositeUnique<Bar>(modelBuilder, "IX_R_S", t => new {t.R, t.S});

我在想这样的事情:

代码语言:javascript
复制
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 }));
    }
}

但我有一些问题:

  1. 这是个好主意吗
  2. 如何知道该属性是否为字符串?
  3. 传递参数的最佳方法是什么?
  4. 我如何进入"T"?我在.Entity<typeof(T)>上得到了一个编译错误
EN

回答 1

Stack Overflow用户

发布于 2014-10-09 21:03:59

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

代码语言:javascript
复制
public static class ExtensionMethods
{
    public static StringPropertyConfiguration HasIndex(this StringPropertyConfiguration config, string indexName, int i)
    {
        return config.HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation((new IndexAttribute(indexName, i) { IsUnique = true })));
    }

    public static PrimitivePropertyConfiguration HasIndex(this PrimitivePropertyConfiguration config, string indexName, int i)
    {
        return config.HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation((new IndexAttribute(indexName, i) { IsUnique = true })));
    }
}

用法如下:

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26283537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档