C# System.InvalidOperationException:&x27;实体类型'';处于阴影状态。有效的模型要求所有实体类型都具有相应的CLR类型;
我正在处理ASP.NET核心web API项目,但当我运行web应用程序时,出现以下错误: System.InvalidOperationException:“实体类型”词汇表处于 影子国家。有效的模型要求所有实体类型都具有 相应的CLR类型。' 我使用的是实体框架核心2。问题在于C# System.InvalidOperationException:&x27;实体类型'';处于阴影状态。有效的模型要求所有实体类型都具有相应的CLR类型;,c#,entity-framework-core,asp.net-core-webapi,C#,Entity Framework Core,Asp.net Core Webapi,我正在处理ASP.NET核心web API项目,但当我运行web应用程序时,出现以下错误: System.InvalidOperationException:“实体类型”词汇表处于 影子国家。有效的模型要求所有实体类型都具有 相应的CLR类型。' 我使用的是实体框架核心2。问题在于VocabularyManager类中的GetAll方法 [Route("api/vocabulary")] public class VocabularyController : ControllerBase {
VocabularyManager
类中的GetAll
方法
[Route("api/vocabulary")]
public class VocabularyController : ControllerBase
{
[HttpGet()]
public IActionResult GetAllVocabulary()
{
var vocab = _iRepo.GetAll();
return new JsonResult(vocab)
{
StatusCode = 200
};
}
}
公共类词汇管理器:IDataRepository
{
应用上下文ctx;
公共词汇管理器(ApplicationContext c)
{
ctx=c;
}
公共空白添加(词汇表B)
{
ctx.Add(vocab);
ctx.SaveChanges();
}
公共无效删除(int-id)
{
抛出新的NotImplementedException();
}
公共词汇表Get(int-id)
{
var词汇=ctx.Vocabularies.FirstOrDefault(c=>c.Id==Id);
返回词汇;
}
公共IEnumerable GetAll()
{
var restVocab=ctx.Vocabularies.ToList();
返回b;
}
公共void更新(int-id,词汇表b)
{
抛出新的NotImplementedException();
}
}
问题可能是由于您在modelBuilder.entity
中对实体使用了错误的类型。您应该在modelBuilder.Entity
中使用System.Type
,我们可以在OnModelCreating
方法中查看您的词汇表
类以及如何配置它(如果存在)?
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();
}
}