I need to update two tables with a one-to-many relationship in my database.
This is my Course
model:
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
// Relationships
public virtual ICollection<MajorCourse>? MajorsCourses { get; set; }
This is my MajorCourse
model:
[Key]
public int Id { get; set; }
public int NumberOfUnits { get; set; }
public int CourseId { get; set; }
public virtual Course? Course { get; set; }
public int MajorId { get; set; }
public virtual Major? Major { get; set; }
public virtual ICollection<UserMajorCourse>? UsersMajorsCourses { get; set; }
This is MajorCourseVM
:
public int NumberOfUnits { get; set; }
public int CourseId { get; set; }
public int MajorId { get; set; }
public string Name { get; set; }
public string GradeId { get; set; }
Here is my attempt:
public async Task<IActionResult> Edit(MajorCourseVM majorCourseVM)
{
if (!ModelState.IsValid)
{
return View(majorCourseVM);
}
Course course = new Course();
course.Name = majorCourseVM.Name;
course.Id = majorCourseVM.CourseId;
MajorCourse majorCourse = new MajorCourse();
majorCourse.MajorId = majorCourseVM.MajorId;
majorCourse.CourseId = majorCourseVM.CourseId;
majorCourse.NumberOfUnits = majorCourseVM.NumberOfUnits;
_context.Attach(course);
_context.Entry(course).State = EntityState.Modified;
_context.Attach(majorCourse);
_context.Entry(majorCourse).State = EntityState.Modified;
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
But I get the following error:
The property 'Course.Id' has a temporary value while attempting to change the entity's state to 'Modified'. Either set a permanent value explicitly, or ensure that the database is configured to generate values for this property.
I couldn't find a solution and don't know what to do!
But I get the following error:
The property 'Course.Id' has a temporary value while attempting to change the entity's state to 'Modified'. Either set a permanent value explicitly, or ensure that the database is configured to generate values for this property.
The error is pretty obvious and the exception usually happened when you try to add/update an entity with a foreign key that does not exists.
Most importantly while updating existing object you should get the object by existing Id then need to update the the current data with it. But in your shared code snippet I can see you are creating new object which causing the issue because, it will certainly encounter conflict with the fogerign key which you are having now. Instead you should pass object by its Id in order to update existing object
You should do as following:
public async Task<IActionResult> Edit(MajorCourseVM majorCourseVM)
{
if (!ModelState.IsValid)
{
return View(majorCourseVM);
}
var dbContextCourse = _context.Course
.Include(s => s.MajorsCourses)
.FirstOrDefault(s => s.Id.Equals(majorCourseVM.Id));
dbContextCourse.Name = majorCourseVM.Name;
dbContextCourse.MajorsCourses = majorCourseVM.majorCourse;
//Same goes for other entities as well.
//You can execute a disconnected update operation here.
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
Note: Above example has been made for your understanding, just made necessary changes in order to fit within your scenario.Please refer to this official document for more clarity.