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. Save Foreign Key Value - EF Core + ASP.NET Core

Save Foreign Key Value - EF Core + ASP.NET Core

Scheduled Pinned Locked Moved ASP.NET
asp-nethelpcsharphtmldatabase
1 Posts 1 Posters 0 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.
  • V Offline
    V Offline
    vanilsonwdd
    wrote on last edited by
    #1

    Hi guys, I´m new to ASP.NET Core and EF Core and I have the following problem: I have two related objects, Goals and Months, Goals got the foreign key of Months, and when I try to save the data of goals I got a foreign key error because the value of MonthId is 0, but when I create the select in the HTML I got correct code value. Description of the error: SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint FK_Goals_MonthId;. The conflict occurred in database Portal_RNT, table dbo.Months , column MonthId;. The statement has been terminated. Here is the code: HTML

    @model ProjectName.Models.Goals

    <form asp-controller="Goals" asp-action="Create">
    Other form elements

      <select name="months" asp-for="MonthId" class="form-control" asp-items="ViewBag.Months">
    
      </select>
    
      <input type="submit" value="Save" class="btn btn-primary" />
    

    </form>

    Goals Model

    public class Goals
    {
    [Display(Name = "Code")]
    public Int32 GoalId { get; set; }

        public String GoalName{ get; set; }
        public Months Month{ get; set; }
        public Int32 MonthId{ get; set; }
        ....
        Other properties
    
    }
    

    Month Model

    public class Months
    {
        \[Key\]
        \[Display(Name = "Code")\]
        public Int32 MonthId { get; set; }
        \[Display(Name = "Designation")\]
        public String Designation{ get; set; }
    }
    

    Goals Controller

    public IActionResult Create(Goals goal)
    {
    if (ModelState.IsValid)
    {
    _context.Goals.Add(goal);
    _context.SaveChanges();
    }
    return View("~/../Create.cshtml");
    }

    Best Regards

    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