I'm trying to dynamically switch out my table annotations' schema values @ Runtime when using EF6.
So here's what I've got thus far:
var builder = new DbModelBuilder()
var dbSetProperties = typeof(T).GetProperties().Where(p => p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>));
foreach (PropertyInfo property in dbSetProperties)
{
Type[] propTypes = property.PropertyType.GetGenericArguments();
// Iterate the DbSets and set the correct schema
foreach (Type dbSetType in propTypes)
{
// Get the TableAttribute
var tableAttribute = Attribute.GetCustomAttribute(dbSetType, typeof(TableAttribute));
MethodInfo dbModelMethodInfo = typeof(DbModelBuilder).GetMethod("Entity");
MethodInfo entityTypeConfigMethodInfo = typeof(EntityTypeConfiguration<>).GetMethod("ToTable", new[] { typeof(String), typeof(String) });
MethodInfo genericDbModelMethodInfo = dbModelMethodInfo.MakeGenericMethod(dbSetType);
genericDbModelMethodInfo.Invoke(builder, null);
entityTypeConfigMethodInfo.Invoke(genericDbModelMethodInfo, new Object[] { (tableAttribute as TableAttribute).Name, "NEW_SCHEMA_VALUE" });
}
}
What I'm trying to accomplish is something like this (which doesn't work):
builder.Entity<dbSetType>().ToTable((tableAttribute as TableAttribute).Name, "NEW_SCHEMA_VALUE");
Basically, for T I want to pull the DbSets, determine the Class used in the Entity<> generic, get the TableAttribute, and set the Schema to a new value.
Currently, on entityTypeConfigMethodInfo.Invoke, I'm getting an error of "Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true".
What am I missing?
Aucun commentaire:
Enregistrer un commentaire