Dynamically loading DbSet to DbContext

c# entity-framework entity-framework-core

Question

How do I dynamically load into an EF7 DbContext a X number of classes without explicitly writing them in a DbContext class ?

For example, I tried to avoid it like this:

public class MyDbContextClass : DbContext
{
    public DbSet<Category> categories {get;set;}
    public DbSet<Product> products {get;set;}
    ...
}

So it could be great to load Category and Product dynamically (without knowing if I have 2 or 20 classes).

Is it possible?

1
2
8/9/2016 3:16:21 PM

Popular Answer

dbContext.Set<T>() creates an instance of DbSet<T> as long as T is a type in your model (i.e. you have to add the entity type in OnModelCreating).

See the source code for .Set<T>.

2
8/9/2016 3:23:20 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow