0

我有一個像下面有關兩種型號:ASP.Net核心要求相關車型領域的錯誤

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() 
     { 

     } 
    } 

我有一個存儲庫上下文

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"做POST或PUT類似然後它給了我錯誤:

{ 
    "Connections[0].Context.Role": [ 
    "The Role field is required." 
    ] 
} 

爲什麼會這樣呢?我希望能夠POST或PUT數據,即使我沒有填寫connections的相關字段。現在我只有控制器上下文

UPDATE:控制器:

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

爲什麼你需要上下文的連接類當你有contextId,如果你拿出那對你有用的願望? – Turbot

+0

這是我第一次使用asp.net核心,並且我正在關注模型關係教程(https://docs.microsoft.com/en-us/ef/core/modeling/relationships),它是在那裏給出的。我正在構建Web Api,在這種情況下,不需要此導航屬性? – Nitish

+0

@Nitish你能告訴我控制器類嗎? – Win

回答

0

這是因爲你已經在你的關係字段錯過虛擬關鍵字。在這裏添加虛擬關鍵字連接領域Context類

public virtual ICollection<Connection> Connections { get; set; } 

,並在Connection類的上下文將解決這一問題

public virtual Context Context { get; set; } 

欲瞭解更多信息閱讀this anwser

+0

添加虛擬機後,是否需要遷移和更新數據庫?另外,公共虛擬上下文上下文{get;組; }如果我正在構建Web API(指的是對我的問題的第一條評論),那麼在所有必要的情況下? – Nitish

+0

不這麼認爲......實際上它取決於你的映射..你有沒有將連接中的ContextId字段映射爲db中的外鍵 – CPR43

+0

不,不明確。在問題中看到我的模型,這就是你指的是對的? – Nitish