You cannot manually set the value of an Identity (Auto Increment) column.
Here, in the below example PurchaseId is an Identity column.
purchase.PurchaseId = 6;
_dbContext.Purchases.Add(purchase);
_dbContext.SaveChanges();
When saving changes to the database the EF Core throws Microsoft.EntityFrameworkCore.DbUpdateException with Cannot insert explicit value for identity column in table 'Purchases' when IDENTITY_INSERT is set to OFF. error.
So, by default, you cannot assign the value.
But this is not the issue here.
The bigger issue is that you are exposing the Domain classes to your client. This flaw in API design can lead to more than this problem.
You have to create a DTO e.g. CreateItemDTO whose only responsibility is to contain all the methods required to create an Item in the database.
The same way you should not expose Item class in the GET request. This leads to a problem when API related columns that are not for clients gets exposed. Create a GetItemDTO which would only contain information that is important for the GET request.
CreateItemDTO
public class CreateItemDTO
{
[Required(ErrorMessage = "You must provide a name.")]
[MaxLength(255)]
public string Name { get; set; }
}
Read Exposing domain models over API for more information.