Reputation: 3640
I'm struggling to update a related entity in my Edit Post action.
I have a Job and a JobNotes model that look like this:
public class Job
{
public Job()
{
JobNotes = new List<JobNote>();
}
[Key]
public int JobID { get; set; }
public string jobName { get; set; }
public ICollection<JobNote> JobNotes { get; set; }
}
public class JobNote
{
[Key]
public int JobNoteID { get; set; }
public string Author { get; set; }
public string Note { get; set; }
public Job Job { get; set; }
}
I've also used Fluent API to map my relationships:
(Any feedback as to whether I've done this correctly is most welcome!)
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Job>()
.HasMany(x => x.JobNotes)
.WithOptional(y => y.Job);
}
And the problem is that in my Post method my JobNotes object is being added to the parent Job object, but not being saved to the database.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(JobViewModel model)
{
if (ModelState.IsValid)
{
var existingJob = db.Jobs
.Include(x => x.JobNotes)
.Single(c => c.JobID == model.Job.JobID);
model.Job.JobNotes.Add(new JobNote
{
Author = "System",
Note = "Job modified by " + User.Identity.Name
});
db.Entry(existingJob).CurrentValues.SetValues(model.Job);
db.SaveChanges();
return RedirectToAction("Details", new { id = model.Job.JobID });
}
}
What have I done wrong? Thanks in advance for your help.
Upvotes: 1
Views: 1166
Reputation: 177163
Use the existingJob
instead of model.Job
to add the new JobNote
:
existingJob.JobNotes.Add(...);
When SaveChanges
is called EF detects the new note (based on the already attached existingJob
) and will add it to the context and then insert into the database.
You don't need, by the way, to include the existing notes in this procedure, thus you can remove the .Include(x => x.JobNotes)
.
Upvotes: 2