When you use the DbContext.Set () method, the following exception is raised:
The entity type is not part of the model for the current context
If I create a DbSet directly in the DatabaseContext, it works. But my current need is to create a generic repository, and from it get the DbSet for the entity passed in parameter, as the code below shows.
What is wrong with the implementation to raise this exception? I'm using CodeFirst.
Controller
public class QualquerController<TEntidade> : Controller
{
public DatabaseContext contexto;
public Repositorio<TEntidade> repositorio;
public QualquerController()
{
repositorio = new Repositorio<TEntidade>(contexto);
}
}
Repository
public class Repositorio<TEntidade> : where TEntidade : EntidadeBase
{
public DbSet<TEntidade> Entidade;
public Repositorio(DatabaseContext contexto)
{
Entidade = contexto.Set<TEntidade>();
}
}