Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Insert Statement Conflict on Foreign Key

$
0
0

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.


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>