0
When using 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 Databasecontext, it works. But my current need is to create a generic repository, and from it get the Dbset referring to entity passed in parameter, as shown in the code below.
What’s wrong with the implementation to lift 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>();
}
}
forehead without that line
Entidade = contexto.Set<TEntidade>();
. I don’t think you need this– Lucas Venturella
Hi Lucas, I’m not in my Dbcontext, so I need this line to instantiate the Dbset. If I shoot, gives Nullreferenceexception.
– Vinícius
This entity you are trying to add already exists in this context?
– Lucas Venturella