我正在尝试添加一些值以及尝试从我的数据库中删除选定的值。
我正在使用如下代码:
[HttpPost]
public ActionResult SavePlaylist(List<ItemEditViewModel> content, long playlistid, List<long> deleted, string Title)
{
var playlist = db.Playlists.Include("PlaylistContents").FirstOrDefault(x => x.PlaylistId == playlistid);
for (int i = 0; i < content.Count; i++)
{
var pc = new PlaylistContent();
pc.Sequence = content[i].MetaID;
playlist.PlaylistContents.Add(pc);
}
for (int i = 0; i < deleted.Count; i++)
{
long delid = deleted[i];
ar remove = playlist.PlaylistContents.FirstOrDefault(x => x.PlaylistContentId.Equals(delid));
playlist.PlaylistContents.Remove(remove);
}
db.SaveChanges();
return JSON(playlist);
}
值已成功添加但在从中删除值时,错误显示如下:
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.
我能做些什么来解决这个错误。业务逻辑中是否存在任何类型的错误。