1

I have been searching as to why my problem is happening and I'm stumped. Not sure if it's how I did my data annotations or what... tried several things, now just hoping it's something one of you smarties can spot.

We are using EF5, MVC4 Razor and MVCScaffolding. I'm still pretty new to it all.

My problem is, when the form submits and the Save method is called, the model is posted with all the correct data, but it appears that instead of just saving the probability object it is trying to fist do an update on the Component table (which is a FK to Probabilities). This update has no values passed in (id = 0) so the page fails due to concurrency checking. AND we were only able to find this out by using SQL Profiler.

Can someone tell me what I am doing wrong, as I don't believe the FK tables should be updating. Please see all my code below... Any help or guidance is much appreciated :)

My Model looks like this:

public class Probability 
{
    [Key]
    public int ProbabilityId { get; set; }

    public int ComponentId { get; set; }
    [ForeignKey("ComponentId")]
    public virtual Component Component { get; set; }

    public int PlantId { get; set; }
    [ForeignKey("PlantId")]
    public virtual Plant Plant { get; set; }

    // Data Entered into the database
    public decimal RiskMinorPercentage { get; set; }
    public decimal RiskMajorPercentage { get; set; }
    public decimal RiskCatastrophicPercentage { get; set; }

We're trying to use a Telerik Grid to load the data and edit it. Data loads and is editable and the data returns to the probabilityController which calls the probabilityReposity.

probabilityController

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Save(Probability probability)
    {
        if (TryUpdateModel(probability))
        {

            probabilityRepository.InsertOrUpdate(probability);
            probabilityRepository.Save();     

        }

        return RedirectToAction("Index", probabilityRepository.All.ToList());
    }

and probabilityRepository

     public void InsertOrUpdate(Probability probability)
    {
        if (probability.ProbabilityId == default(int)) {
            context.Probabilities.Add(probability);
        } else
        {
              context.Entry(probability).State = EntityState.Modified;
        }
    }
tereško
  • 58,060
  • 25
  • 98
  • 150

0 Answers0