public class User { [Key] public required string Guid { get; set; } [Required] public required string Name { get; set; } [Required] public required string Tag { get; set; } public DateTime LastUpdate { get; set; } }
public class User { [Key] public required string Guid { get; set; } [Required] public required string Name { get; set; } [Required] public required string Tag { get; set; } public DateTime LastUpdate { get; set; } }
And here's my code for the Controller. Note that I am assigning the primary key within this method.
[HttpPost]public async Task<ActionResult<User>> PostUser(User user){ User newUser = new User() { Guid = Guid.NewGuid().ToString(), Name = name, Tag = tag, LastUpdate = DateTime.Now }; _context.Users.Add(newUser); return CreatedAtAction(nameof(GetUsers), user);}
[HttpPost]public async Task<ActionResult<User>> PostUser(User user){ User newUser = new User() { Guid = Guid.NewGuid().ToString(), Name = name, Tag = tag, LastUpdate = DateTime.Now }; _context.Users.Add(newUser); return CreatedAtAction(nameof(GetUsers), user);}
My problem is that I do not want the primary key (Guid) to be required in the request body to send the POST request. Is there any way around this?
I just want my POST request body to look like the following example:
{ "Name": "Jimbo", "Tag": 12345}
{ "Name": "Jimbo", "Tag": 12345}
Omitting the Guid from this results in the following error:
"JSON deserialization for type 'api.Models.User' was missing required properties, including the following: guid"
"JSON deserialization for type 'api.Models.User' was missing required properties, including the following: guid"