Sto lavorando a un progetto API Web ASP.NET Core, ma quando eseguo la mia applicazione Web, ottengo questo errore:
System.InvalidOperationException: 'Tipo di entità' Vocabolario 'è in stato ombra. Un modello valido richiede che tutti i tipi di entità abbiano il tipo CLR corrispondente. "
Sto usando Entity Framework Core 2. Il problema è con il metodo GetAll
nella classe VocabularyManager
.
[Route("api/vocabulary")]
public class VocabularyController : ControllerBase
{
[HttpGet()]
public IActionResult GetAllVocabulary()
{
var vocab = _iRepo.GetAll();
return new JsonResult(vocab)
{
StatusCode = 200
};
}
}
public class VocabularyManager : IDataRepository
{
ApplicationContext ctx;
public VocabularyManager(ApplicationContext c)
{
ctx = c;
}
public void Add(Vocabulary vocab)
{
ctx.Add(vocab);
ctx.SaveChanges();
}
public void Delete(int id)
{
throw new NotImplementedException();
}
public Vocabulary Get(int id)
{
var vocabulary = ctx.Vocabularies.FirstOrDefault(c => c.Id == id);
return vocabulary;
}
public IEnumerable<Vocabulary> GetAll()
{
var restVocab = ctx.Vocabularies.ToList();
return restVocab;
}
public void Update(int id, Vocabulary vocab)
{
throw new NotImplementedException();
}
}
Il problema potrebbe essere dovuto al fatto che stavi usando un tipo sbagliato per la tua entità in modelBuilder.Entity
. Dovresti usare System.Type
nel modelBuilder.Entity
.