2021年1月23日星期六

How to create a new record in the entity framework without specifying the primary key in its parameters?

I have a webpage in my project where a person signs up to be a user. My controller then gets an api call from the frontend with the post values entered from the signup. I am trying to create a new record in my database with that info, is there anyway to create an object without specifying the primary key in the parameters? I obviously am not taking in the id from the user so I just want to create the object without the id.

Controller

// POST api/values          [HttpPost]          public void Post(string username, string password, string email, string role)          {              Users user = new Users(username, password, email, role);              _repository.CreateUser(user);              _repository.SaveChanges();          }  

Model

using System.ComponentModel.DataAnnotations;    namespace IssueTracker.Models  {      public class Users      {          [Key]          public int id { get; set; }            [Required]          public string username { get; set; }            [Required]          public string password { get; set; }            [Required]          public string email { get; set; }            [Required]          public string role { get; set; }            public Users(int id, string username, string password, string email, string role)          {              this.id = id;              this.username = username;              this.password = password;              this.email = email;              this.role = role;          }            public Users()          {            }      }  }  
https://stackoverflow.com/questions/65866600/how-to-create-a-new-record-in-the-entity-framework-without-specifying-the-primar January 24, 2021 at 10:22AM

没有评论:

发表评论