【发布时间】:2017-03-23 01:29:18
【问题描述】:
我有两个相关的模型,如下所示:
public class Context
{
[Key]
public long ContextId { get; set; }
[Required]
public string Role { get; set; }
public ICollection<Connection> Connections { get; set; }
public Context()
{
}
}
和
public class Connection
{
[Key]
public long ConnectionId { get; set; }
public string Name { get; set; }
public long ContextId { get; set; }
public Context Context { get; set; }
public Connection()
{
}
}
我有一个 Context 的存储库:
public class ContextRepository: IContextRepository
{
private readonly WebAPIDataContext _db;
public ContextRepository(WebAPIDataContext db)
{
_db = db;
}
public Context CreateContext(Context context)
{
_db.Contexts.Add(context);
_db.SaveChanges();
return context;
}
public void DeleteContext(long id)
{
Context context = GetContext(id);
if (context != null)
{
_db.Contexts.Remove(context);
_db.SaveChanges();
}
}
public List<Context> GetAllContexts()
{
return _db.Contexts
.Include(context => context.Connections)
.ToList();
}
public Context GetContext(long id)
{
return _db.Contexts.FirstOrDefault(o => o.ContextId == id);
}
public void UpdateContext(long id, Context context)
{
Context contextToUpdate = _db.Contexts.FirstOrDefault(o => o.ContextId == id);
contextToUpdate.Role = context.Role;
contextToUpdate.Connections = context.Connections;
_db.Update(contextToUpdate);
_db.SaveChanges();
}
}
Swagger 给了我如下骨架:
{
"contextId": 0,
"role": "string",
"connections": [
{
"connectionId": 0,
"name": "string",
"contextId": 0,
"context": {}
}
]
}
如果我填写 "Role":"Worker" 和 "name":"Max" 并在 PUT 中执行 POST 或类似操作,那么它会给我错误:
{
"Connections[0].Context.Role": [
"The Role field is required."
]
}
为什么会这样?即使我不填写connections 相关字段,我也希望能够发布或放置数据。现在我只有 Context 的控制器。
更新:控制器:
[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();
}
}
【问题讨论】:
-
当你有 contextId 时,为什么你需要在你的连接类中使用上下文,如果你拿出对你有用的东西?
-
这是我第一次使用 asp.net core,我正在关注模型关系教程 (docs.microsoft.com/en-us/ef/core/modeling/relationships),它是在那里给出的。我正在构建 Web Api,在这种情况下不需要此导航属性?
-
@Nitish 你能告诉我控制器类吗?
-
@Turbot 我用控制器更新了我的问题。此外,如果我从连接中删除上下文,错误就消失了。但是,我在 repo 中的更新方法仍然存在某种错误。如果我尝试将更新的数据放入现有连接(在特定上下文中),则会引发错误。
-
你能发布服务器异常吗?
标签: asp.net-core entity-framework-core asp.net-core-webapi