Home > other >  Entity Framework, WebApi not allow post of table ID
Entity Framework, WebApi not allow post of table ID

Time:03-04

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

CodePudding user response:

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; }

  • Related