[SOLVED] Entity Framework, WebApi not allow post of table ID

Issue

I have this basic project that I’m using to learn ASP.NET and I’m using Entity Framework to talk to the database.

I have this simple Table, and when I make a post the API to create a new equipa, I don’t want to let the user specify the id.

I’ve read the Microsoft docs (https://docs.microsoft.com/en-us/ef/core/modeling/generated-properties?tabs=data-annotations) and I’ve tried using the data annotation

[DatabaseGenerated(DatabaseGeneratedOption.Identity)] 

on my model class, but it still allows the user to define the equipa id in the (API Post).

How do I make that the user isn’t allowed to define the id and let PostgreSQL generate the id itself?

My controller

Solution

When creating the database table set the id column as identity: https://www.postgresqltutorial.com/postgresql-identity-column/

Also define your property in the model with only { get; }

Answered By – Egil Hansen

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *