Asp.net core ASP.Net核心相关模型字段必填错误
我有两个相关模型,如下所示:Asp.net core ASP.Net核心相关模型字段必填错误,asp.net-core,entity-framework-core,asp.net-core-webapi,Asp.net Core,Entity Framework Core,Asp.net Core Webapi,我有两个相关模型,如下所示: public class Context { [Key] public long ContextId { get; set; } [Required] public string Role { get; set; } public ICollection<Connection> Connections { get; set; } public Co
public class Context
{
[Key]
public long ContextId { get; set; }
[Required]
public string Role { get; set; }
public ICollection<Connection> Connections { get; set; }
public Context()
{
}
}
{
"contextId": 0,
"role": "string",
"connections": [
{
"connectionId": 0,
"name": "string",
"contextId": 0,
"context": {}
}
]
}
我有一个上下文存储库:
如果我填写了“角色”:“工作人员”
和“姓名”:“Max”
并在PUT中执行POST或类似操作,则会出现错误:
{
"Connections[0].Context.Role": [
"The Role field is required."
]
}
为什么会这样?即使我没有填写连接
相关字段,我也希望能够发布或放置数据。现在我只有用于上下文的控制器
更新:控制器:
[Route("api/[controller]")]
public class ContextController : Controller
{
private readonly IContextRepository _contexts;
public ContextController(IContextRepository contexts)
{
_contexts = contexts;
}
[HttpGet("")]
public IActionResult GetAllContexts()
{
try
{
List<Context> contexts = _contexts.GetAllContexts();
return Ok(contexts);
}
catch (EntityNotFoundException<Context>)
{
return NotFound();
}
}
[HttpGet("{id}")]
public IActionResult GetContext(long id)
{
Context context= _contexts.GetContext(id);
if (context == null)
{
return NotFound();
}
return Ok(context);
}
[HttpPost]
public IActionResult CreateContext([FromBody] Context context)
{
if (ModelState.IsValid == false)
{
return BadRequest(ModelState);
}
Context createdContext= _contexts.CreateContext(context);
if (createdContext== null)
{
return NotFound();
}
return CreatedAtAction(
nameof(GetContext), new { id = createdContext.ContextId}, createdContext);
}
[HttpPut("{id}")]
public IActionResult UpdateContext(long id, [FromBody] Context context)
{
if (ModelState.IsValid == false)
{
return BadRequest(ModelState);
}
try
{
_contexts.UpdateContext(id, context);
return Ok();
}
catch (EntityNotFoundException<Context>)
{
return NotFound();
}
}
[HttpDelete("{id}")]
public IActionResult DeleteCOntext(long id)
{
_contexts.DeleteContext(id);
return Ok();
}
}
[路由(“api/[控制器]”)]
公共类ContextController:控制器
{
专用只读IContextRepository\u上下文;
公共上下文控制器(IContextRepository上下文)
{
_上下文=上下文;
}
[HttpGet(“”)
公共IActionResult GetAllContexts()
{
尝试
{
列表上下文=_contexts.GetAllContexts();
返回Ok(上下文);
}
捕获(EntityNotFoundException)
{
返回NotFound();
}
}
[HttpGet(“{id}”)]
公共IActionResult GetContext(长id)
{
Context=\u Context.GetContext(id);
if(上下文==null)
{
返回NotFound();
}
返回Ok(上下文);
}
[HttpPost]
公共IActionResult CreateContext([FromBody]上下文)
{
if(ModelState.IsValid==false)
{
返回请求(ModelState);
}
Context createdContext=_Context.CreateContext(Context);
if(createdContext==null)
{
返回NotFound();
}
返回CreateDataAction(
nameof(GetContext),新的{id=createdContext.ContextId},createdContext);
}
[HttpPut(“{id}”)]
公共IActionResult UpdateContext(长id,[FromBody]上下文)
{
if(ModelState.IsValid==false)
{
返回请求(ModelState);
}
尝试
{
_context.UpdateContext(id,context);
返回Ok();
}
捕获(EntityNotFoundException)
{
返回NotFound();
}
}
[HttpDelete(“{id}”)]
公共IActionResult DeleteCOntext(长id)
{
_contexts.DeleteContext(id);
返回Ok();
}
}
这是因为您在关系字段中遗漏了virtual关键字。此处将虚拟关键字添加到上下文类中的连接字段
public virtual ICollection<Connection> Connections { get; set; }
欲了解更多信息,请阅读当您拥有contextId时,为什么您需要连接类中的上下文,如果您拿出了对您有用的上下文,请阅读。这是我第一次使用asp.net core,我正在学习模型关系教程(),这是在那里给出的。我正在构建Web Api,在这种情况下,不需要此导航属性?@Nitish能否向我展示控制器类?@Turbot我用控制器更新了我的问题。另外,如果我从连接中删除了上下文,错误就消失了。然而,我在回购协议中的更新方法仍然是错误的。如果我尝试将更新的数据放入现有连接(在特定上下文中),则会引发错误。您可以发布服务器异常吗?添加虚拟连接后是否需要迁移和更新数据库?另外,如果我正在构建Web API(参考我问题的第一条注释),
公共虚拟上下文{get;set;}
是否有必要。。。实际上,这取决于您的映射。。是否已将连接中的ContextId字段映射为dbNo中的外键,而不是显式映射。在问题中查看我的模型,这就是您所指的,对吗?如果您在connectionOkay中为上下文字段添加[ForeignKey(“ContextId”)]属性,这会很好,如果它是web API,我真的需要连接中的public Context Context{get;set;}
?因为只有在删除它之后,我才能了解原始错误(请参见我问题下方的评论)
[Route("api/[controller]")]
public class ContextController : Controller
{
private readonly IContextRepository _contexts;
public ContextController(IContextRepository contexts)
{
_contexts = contexts;
}
[HttpGet("")]
public IActionResult GetAllContexts()
{
try
{
List<Context> contexts = _contexts.GetAllContexts();
return Ok(contexts);
}
catch (EntityNotFoundException<Context>)
{
return NotFound();
}
}
[HttpGet("{id}")]
public IActionResult GetContext(long id)
{
Context context= _contexts.GetContext(id);
if (context == null)
{
return NotFound();
}
return Ok(context);
}
[HttpPost]
public IActionResult CreateContext([FromBody] Context context)
{
if (ModelState.IsValid == false)
{
return BadRequest(ModelState);
}
Context createdContext= _contexts.CreateContext(context);
if (createdContext== null)
{
return NotFound();
}
return CreatedAtAction(
nameof(GetContext), new { id = createdContext.ContextId}, createdContext);
}
[HttpPut("{id}")]
public IActionResult UpdateContext(long id, [FromBody] Context context)
{
if (ModelState.IsValid == false)
{
return BadRequest(ModelState);
}
try
{
_contexts.UpdateContext(id, context);
return Ok();
}
catch (EntityNotFoundException<Context>)
{
return NotFound();
}
}
[HttpDelete("{id}")]
public IActionResult DeleteCOntext(long id)
{
_contexts.DeleteContext(id);
return Ok();
}
}
public virtual ICollection<Connection> Connections { get; set; }
public virtual Context Context { get; set; }