locked
Insert Statement Conflict on Foreign Key RRS feed

  • Question

  • User1503189645 posted

    First try of a site outside the safety of tutorials.  I am getting this error:

    SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Equipments_Sites_SiteID". The conflict occurred in database "aspnet-ConditionReport", table "dbo.Sites", column 'SiteID'. The statement has been terminated.

    I don't understand what is causing this error.  I cant see an issue.  The model is:

     public class Equipment
        {
            [Key]
            public int EQDataID { get; set; }
    
    
            [Display(Name ="Inspection Date")]
            [DataType(DataType.Date)]
            public DateTime InspectDate { get; set; }
    
            [Display(Name ="Site")]
            public int SiteID { get; set; }
    
            [Display(Name ="Water Body")]
            public Nullable<int> WaterBodyID { get; set; }
    
            [Display(Name = "Equipment Type")]
            public int EqTypeID { get; set; }
    
            [Display(Name = "Manufacturer")]
            public int MakeID { get; set; }
    
            [Required]
            public string Model { get; set; }
    
          
            [Display(Name = "Serial Number")]
            public string SerialNumber { get; set; }
            public byte[] Photo { get; set; }
    
            [Display(Name = "Estimated Install Date")]
            [DataType(DataType.Date)]
            public DateTime? InstallDate { get; set; }
    
            [Display(Name = "Estimated Remaining Life")]
            public int EstimateRemainingLife { get; set; }
    
            [Display(Name = "Last Service Date")]
            [DataType(DataType.Date)]
            public DateTime? LastServiceDate { get; set; }
            
            [Display(Name ="What is Your Recommendation")]
            public string Recommended { get; set; }
            public string Notes { get; set; }
    
            [Display(Name = "Condition")]
            public int ConditionID { get; set; }
    
            [Display(Name = "Replacement Cost")]
            [DataType(DataType.Currency)]
            public float? ReplacementCost { get; set; }
    
    
            public Site Site { get; set; }
            public WaterBody Waterbody { get; set; }
            public EqType Eqtype { get; set; }
            public Make Make { get; set; }
    
            public Condition Condition { get; set; }
    
        }

    The controller is:

     // POST: Equipments/Create
            // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
            // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> Create([Bind("EQDataID,ConditionID,EqTypeID,EstimateRemainingLife,InspectDate,InstallDate,LastServiceDate,MakeID,Model,Notes,Photo,Recommended,ReplacementCost,SerialNumber,WaterBodyID, SiteID")] Equipment equipment)
            {
                if (ModelState.IsValid)
                {
                    _context.Add(equipment);
                    await _context.SaveChangesAsync();
                    return RedirectToAction("Index");
                }
                ViewData["ConditionID"] = new SelectList(_context.Conditions, "ConditionID", "ConditionID", equipment.ConditionID);
                ViewData["EqTypeID"] = new SelectList(_context.EqTypes, "EqTypeID", "EqTypeID", equipment.EqTypeID);
                ViewData["MakeID"] = new SelectList(_context.Makes, "MakeID", "MakeID", equipment.MakeID);
                ViewData["WaterBodyID"] = new SelectList(_context.WaterBodys, "WaterBodyID", "WBName", equipment.WaterBodyID);
                return View(equipment);
            }

    I know its going to be something simple.  But I am just not seeing it and as I have been struggling away for about 4 hours so far I thought I would ask for help.

    Saturday, February 11, 2017 2:30 AM

All replies

  • User-832373396 posted

    Hi dynamictiger,

    Based on your description, I guess that the SiteID value is null or do not exist at table Sites.

    So I suggest that you could debug the line code below for the equipment.SiteID value ,and check the SiteID value whether exist at table Sites via SQL Server.

    public async Task<IActionResult> Create([Bind(" ......., SiteID")] Equipment equipment)
            {//Debug

    Best regards,

    Jolie

    Monday, February 13, 2017 3:13 AM