Getting the following error upon save of my context:
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
The controller code:
#region CreateStory
[HttpPost]
[Authorize]
public ActionResult CreateStory(Userstory story)
{
if (ModelState.IsValid)
{
string _Username = User.Identity.Name.ToLower();
using (var _Db = new NimbleContext())
{
Project _Project = _Db.Projects.First(p => p.Owner.ToLower() == _Username);
if (_Project != null)
{
ProjectGroup _Group = _Db.Groups.First(g => g.GroupID == story.GroupID);
if (_Group != null)
{
_Db.Userstories.Add(story);
_Db.SaveChanges(); <--- Error Occurs here!
return Json(new { Success = true, GroupID = _Group.GroupID });
}
}
}
}
return Json(new { Success = false });
}
#endregion
The Model structure:
Project 1 -> * Groups 1 -> * Userstories
I can prevent the error from occurring by removing the following declarations:
Project _Project = _Db.Projects.First(p => p.Owner.ToLower() == _Username);
ProjectGroup _Group = _Db.Groups.First(g => g.GroupID == story.GroupID);
However I need to make these checks to ensure that the user belongs to the project etc. I really don't see what is causing the error, none of those statements should have an impact on the data to be saved right?