EF entity is not saving child property of same entity type on update

asp.net-mvc asp.net-mvc-4 c# entity-framework entity-framework-6

Accepted Answer

ZZZ_tmp
9
4/3/2017 6:03:09 PM

Popular Answer

Verify the correct configuration of your model:

public class SupportProcess
{
    [Key]
    public int ProcessId { get; set; }
    [DisplayName("Starting process?")]
    public int StartProcessId { get; set; }
    [ForeignKey("StartProcessId")]
    public virtual SupportProcess StartProcess { get; set; }
    public string Name { get; set; }
    [DisplayName("When is this run?")]
    public virtual ProcessSchedule ProcessSchedule { get; set; }
    [DisplayName("")]
    public string Description { get; set; }
    [DisplayName("Expected Result")]
    public string ExpectedResult { get; set; }
}

And how you edit:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(SupportProcessViewModel vm)
{
    if (!ModelState.IsValid)
    {
        return View(vm);   
    {           

    // Get the item
    var sp = db.SupportProcesses.FirstOrDefault(p => p.Id == vm.SupportProcess.ProcessId);

    // Set the new values
    if (vm.SelectedStartProcess > 0)
    {
        sp.StartProcessId = vm.SelectedStartProcess;
    }
    sp.Name = vm.SupportProcess.Name;
    sp.Description = vm.SupportProcess.Description;
    // all the rest values        

    // Save changes
    db.SupportProcesses.Update(sp);
    db.SaveChanges();

    return RedirectToAction("Index");
}    


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow