namespace App.Models
{
public class User
{
public int Id { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public ICollection<Message> SentMessages { get; set; }
public ICollection<Message> ReceivedMessages { get; set; }
}
}
namespace App.Models
{
public class Message
{
public int Id { get; set; }
public string Content { get; set; }
public DateTime CreatedAt {get; set;}
public int FromUserId { get; set;}
public int ToUserId { get; set; }
public User FromUser { get; set; }
public User ToUser { get; set; }
}
}
MessageController.cs
:
public async Task<IActionResult> CreateMessage(Message message)
{
_context.Messages.Add(message);
await _context.SaveChangesAsync();
return Ok(message);
}
请求正文:
{
"Content": "Hi, how are you",
"FromUserId": 1,
"ToUserId": 2
}
我收到此错误:
“类型”:“https://tools.ietf.org/html/rfc9110#section-15.5.1”,
"title": "发生一个或多个验证错误。",
“状态”:400,
“错误”:{
“致用户”:[
“ToUser 字段是必需的。”
],
“来自用户”:[
“FromUser 字段是必需的。”
]
},
}
缺少什么?有人可以帮忙吗?
在类
Message
中,您需要允许空字段FromUser,ToUser
public User? FromUser { get; set; }
public User? ToUser { get; set; }