I have a MeterPeak entity and this entity has a MeterReading entity as a foreign key (included at bottom of question). The MeterReading entity has a composite Primary Key composed of MeterSiteId and DateTime.
So my understanding is that in my MeterPeak entity I must enter a MeterSiteId and a DateTime that matches an existing MeterReading entity to meet the foreign key constraint. I should not be allowed to link to a foreign key that doesn't exist.
However, in the Edit Action of the MeterPeakController I have the following code
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(MeterPeak meterpeak)
{
if (ModelState.IsValid)
{
unitOfWork.MeterPeakRepository.Update(meterpeak);
unitOfWork.Save();
return RedirectToAction("Index");
}
ViewBag.MeterSiteId = new SelectList(unitOfWork.MeterSiteRepository.Get(b => true), "Id", "Location", meterpeak.MeterSiteId);
return View(meterpeak);
}
When I enter a MeterSiteId and a DateTime which do not match an existing meter reading and try to save I would expect the ModelState.IsValid check to return false, but it doesn't. It only fails when it gets to the unitOfWork.save() line and it errors when saving changes to the dbcontext with this error
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_dbo.MeterPeaks_dbo.MeterReadings_MeterSiteId_DateTime"
Why does the ModelState.IsValid not find this problem before getting to the dbcontext.save?
public class MeterPeak
{
[Key]
public int Id { get; set; }
[ForeignKey("PeakReading"), Column(Order = 1)]
public int MeterSiteId { get; set; }
[ForeignKey("PeakReading"), Column(Order = 2)]
public DateTime DateTime { get; set; }
public int? Rating { get; set; }
public String Note { get; set; }
public virtual MeterReading PeakReading { get; set; }
}
public class MeterReading
{
[Key, Column(Order = 1)]
[Required(ErrorMessage = "Please Select a Meter Site")]
public int MeterSiteId { get; set; }
[Key, Column(Order = 2)]
[Required]
public DateTime DateTime { get; set; }
//This is not the Primary key but I need one unique value to assist in getting records, especially from Javascript
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid SingleKey { get; set; }
public virtual MeterSite MeterSite { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:#,##0.00000#}", ApplyFormatInEditMode = true)]
public Double RawLevel { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:#,##0.00000#}", ApplyFormatInEditMode = true)]
public Double RawVelocity { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:#,##0.00000#}", ApplyFormatInEditMode = true)]
public Double RawFlow { get; set; }
[DisplayFormat(DataFormatString = "{0:#,##0.00000#}", ApplyFormatInEditMode = true)]
public Double ValidLevel { get; set; }
[DisplayFormat(DataFormatString = "{0:#,##0.00000#}", ApplyFormatInEditMode = true)]
public Double ValidVelocity { get; set; }
[DisplayFormat(DataFormatString = "{0:#,##0.00000#}", ApplyFormatInEditMode = true)]
public Double ValidFlow { get; set; }
[Range(-1, 3, ErrorMessage = "Rating must be one of the following -1,0,1,2,3")]
public int Rating { get; set; }
public bool? Valid { get; set; }
public virtual ICollection<Note> Notes { get; set; }
}