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

时间:2018-06-03 12:16:16

标签: c# entity-framework-core asp.net-core-webapi

我正在开发asp.net核心Web API项目,但是当我运行我的Web应用程序时,我遇到了这个错误。我正在使用Entity Framework核心2.事实上,它在VocabularyManager类中遇到GetAll方法的问题。

  

System.InvalidOperationException:'实体类型'词汇'在...   影状态。有效模型需要所有实体类型   相应的CLR类型。'

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


    }

1 个答案:

答案 0 :(得分:0)

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