Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to fill Foreign Key in a relational database when creating a new row?

How to fill Foreign Key in a relational database when creating a new row?

Scheduled Pinned Locked Moved ASP.NET
questionasp-netdatabasetutorial
3 Posts 2 Posters 19 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Dunlop
    wrote on last edited by
    #1

    I have a model as follows:

    [Key]
    public int pmId { get; set; }
    [Required]
    public int pmNumber { get; set; }
    [Required]
    [MaxLength(50)]
    public string costCenter { get; set; }
    [Required]
    [MaxLength(15)]
    public string serviceType { get; set; }
    [Required]
    [MaxLength(50)]
    public string destination { get; set; }
    [Required]
    [MaxLength(50)]
    public string workCenter { get; set; }
    [Required]
    public DateTime creationDate { get; set; }
    [Display]
    [Required]
    public DateTime startDate { get; set; }
    [Required]
    public DateTime endDate { get; set; }
    [Required]
    [MaxLength(100)]
    public string mainFileName { get; set; }
    public DateTime? returnDate { get; set; }
    public string? status { get; set; }
    [MaxLength(100)]
    public string? fileName { get; set; }
    public int? uploader { get; set; }
    public bool? isDownloaded { get; set; } = false;

    //Navigation property
    public virtual AppUser user { get; set; }
    //Navigation property
    public PM()
    {

    }

    The Foreign Key is id which is Primary Key of the EF Core Identity table. In a form, I need to insert a new row in PM table. I can fill all necessary columns using the following code:

    var inputElements = new PM()
    {
    pmNumber = Convert.ToInt32(adminModel.pmNumber),
    costCenter = adminModel.costCenter,
    serviceType = adminModel.serviceType,
    destination = adminModel.destination,
    workCenter = adminModel.workCenter,
    creationDate = DateTime.Now,
    startDate = DateTime.Now,
    endDate = DateTime.Now,
    mainFileName = newFileName
    };

    But, because of relational nature of my tables, I need to specify userId when creating a new row in PM table. How can I set Foreign Key? I can detect userId using Session object but I don't know how to insert it in the table.

    L A 2 Replies Last reply
    0
    • A Alex Dunlop

      I have a model as follows:

      [Key]
      public int pmId { get; set; }
      [Required]
      public int pmNumber { get; set; }
      [Required]
      [MaxLength(50)]
      public string costCenter { get; set; }
      [Required]
      [MaxLength(15)]
      public string serviceType { get; set; }
      [Required]
      [MaxLength(50)]
      public string destination { get; set; }
      [Required]
      [MaxLength(50)]
      public string workCenter { get; set; }
      [Required]
      public DateTime creationDate { get; set; }
      [Display]
      [Required]
      public DateTime startDate { get; set; }
      [Required]
      public DateTime endDate { get; set; }
      [Required]
      [MaxLength(100)]
      public string mainFileName { get; set; }
      public DateTime? returnDate { get; set; }
      public string? status { get; set; }
      [MaxLength(100)]
      public string? fileName { get; set; }
      public int? uploader { get; set; }
      public bool? isDownloaded { get; set; } = false;

      //Navigation property
      public virtual AppUser user { get; set; }
      //Navigation property
      public PM()
      {

      }

      The Foreign Key is id which is Primary Key of the EF Core Identity table. In a form, I need to insert a new row in PM table. I can fill all necessary columns using the following code:

      var inputElements = new PM()
      {
      pmNumber = Convert.ToInt32(adminModel.pmNumber),
      costCenter = adminModel.costCenter,
      serviceType = adminModel.serviceType,
      destination = adminModel.destination,
      workCenter = adminModel.workCenter,
      creationDate = DateTime.Now,
      startDate = DateTime.Now,
      endDate = DateTime.Now,
      mainFileName = newFileName
      };

      But, because of relational nature of my tables, I need to specify userId when creating a new row in PM table. How can I set Foreign Key? I can detect userId using Session object but I don't know how to insert it in the table.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You have to save the "key" entity; then you can retrieve the generated key. [Retrieve ID of Inserted Entity](https://entityframework.net/retrieve-id-of-inserted-entity)

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      1 Reply Last reply
      0
      • A Alex Dunlop

        I have a model as follows:

        [Key]
        public int pmId { get; set; }
        [Required]
        public int pmNumber { get; set; }
        [Required]
        [MaxLength(50)]
        public string costCenter { get; set; }
        [Required]
        [MaxLength(15)]
        public string serviceType { get; set; }
        [Required]
        [MaxLength(50)]
        public string destination { get; set; }
        [Required]
        [MaxLength(50)]
        public string workCenter { get; set; }
        [Required]
        public DateTime creationDate { get; set; }
        [Display]
        [Required]
        public DateTime startDate { get; set; }
        [Required]
        public DateTime endDate { get; set; }
        [Required]
        [MaxLength(100)]
        public string mainFileName { get; set; }
        public DateTime? returnDate { get; set; }
        public string? status { get; set; }
        [MaxLength(100)]
        public string? fileName { get; set; }
        public int? uploader { get; set; }
        public bool? isDownloaded { get; set; } = false;

        //Navigation property
        public virtual AppUser user { get; set; }
        //Navigation property
        public PM()
        {

        }

        The Foreign Key is id which is Primary Key of the EF Core Identity table. In a form, I need to insert a new row in PM table. I can fill all necessary columns using the following code:

        var inputElements = new PM()
        {
        pmNumber = Convert.ToInt32(adminModel.pmNumber),
        costCenter = adminModel.costCenter,
        serviceType = adminModel.serviceType,
        destination = adminModel.destination,
        workCenter = adminModel.workCenter,
        creationDate = DateTime.Now,
        startDate = DateTime.Now,
        endDate = DateTime.Now,
        mainFileName = newFileName
        };

        But, because of relational nature of my tables, I need to specify userId when creating a new row in PM table. How can I set Foreign Key? I can detect userId using Session object but I don't know how to insert it in the table.

        A Offline
        A Offline
        Alex Dunlop
        wrote on last edited by
        #3

        I added the following code to PM model:

        public int userId { get; set; }

        and changed the navigation property to the following code:

        //Navigation property
        [ForeignKey("userId ")]
        public virtual AppUser user { get; set; }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups