My DbContext code is similar to this:
public class MyDbContext : DbContext, IMyDbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Product> Products { get; set; }
...
}
And I want to add dbsets in dbcontext dinamically, with a code similar to this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var baseAssemblyName = "Namespace.Web.Model";
var assemblyType = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.FullName.Equals(baseAssemblyName));
//Culture is a class defined in the assembly where my Entity models reside
var typesToRegister = assemblyType.GetTypes()
.Where(type => type.Namespace != null && type.Namespace.Equals(baseAssemblyName))
.Where(type => type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
}
But it is not executing, I put a breakpoint in the beggining of this method and at that point there are building errors in the repository layer that uses for example:
public ICustomer Get(int id)
{
return db.Customers.Find(id);
}
What can i do for create dinamically the entities in dbcontext?
Thanks!