I have many blocks of code that look like the following:
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 }));
This block is telling EF, through fluent API, to create a unique index with columns X
and Y
together, on table Foo
.
Another block of code just like that would be this, with columns R
and S
on table Bar
:
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 }));
I want to refactor this, so that it ends up looking something like:
CreateCompositeUnique<Foo>(modelBuilder, "IX_X_Y", t => new {t.X, t.Y});
CreateCompositeUnique<Bar>(modelBuilder, "IX_R_S", t => new {t.R, t.S});
I was thinking of something like this:
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 }));
}
}
But I have some questions:
- Is this a good idea?
- How do I know if the property is string?
- What is the best way to pass the parameters?
- How do I access "T"? I'm getting a compile error at
.Entity<typeof(T)>