'How do i INSERT into Postgres using Entity Framework .net - problem with models

i am newby in .net and entity and i think i have a very basic problem what gives me headache. I am trying to build a simple crud controller for a Postgres DB using entity framework. The setup is complete and i can get SELECTs via Get using my API.

Now I am trying to use a PUT to INSERT. I am a little confused and don't seem to find the answer. I have a existing Database and created the Datamodel usings Entitys scaffold method. When i try to get my Model data via the POST it gives following error:

{ "errors": { "id": [ "Unable to find a constructor to use for type User. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'id', line 2, position 7." ], "user": [ "The user field is required." ] }, ...

I don't get how i configure the constructor?

Here is my Controller code:

    [HttpPost("user")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> CreateUser(User user)
{


    _dbcontext.Add(user);
    await _dbcontext.SaveChangesAsync();

    var allUsers = _dbcontext.Users.ToList();
    
    return Ok(allUsers);
}

And here is my Model:

using System;
using System.Collections.Generic;

namespace data_services_api.Models
{
    public class User
    {
        
        public User()
        {
            Companies = new HashSet<Company>();
            Employees = new HashSet<Employee>();
            Profiles = new HashSet<Profile>();
        }
        
        public int Id { get; set; }
        public DateTime Created { get; set; }
        public string Email { get; set; } = null!;
        public string? Firstname { get; set; }
        public string? Lastname { get; set; }

        public virtual ICollection<Company> Companies { get; set; }
        public virtual ICollection<Employee> Employees { get; set; }
        public virtual ICollection<Profile> Profiles { get; set; }


    }
    }

I would appreciate your help. Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source