System.InvalidOperationException:"实体类型处于影子状态。有效的模型要求所有实体类型都具有相应的 CLR 类型。



我正在处理一个 ASP.NET Core Web API 项目,但是当我运行我的 Web 应用程序时,我收到此错误:

System.InvalidOperationException: '实体类型'词汇' 在 影子状态。有效的模型要求所有实体类型都具有 对应的 CLR 类型。

我正在使用实体框架核心 2。问题出在VocabularyManager类中的GetAll方法上。

[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();
}
}

问题可能是由于您在modelBuilder.Entity中对实体使用了错误的类型。您应该在modelBuilder.Entity中使用System.Type

最新更新