Reputation: 317
Error Message:
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.
My Code:
foreach (StakeholderApplication stApp in stApplications)
{
StakeholderOutboundDocumentModel currentstake = outboundDocumentModels.Models.FirstOrDefault(u => u.StakeholderApplicationId == stApp.Id);
if (currentstake != null)
{
Dictionary<string, MemoryStream> Attachments = new Dictionary<string, MemoryStream>();
bool hasDocuments = false;
foreach (OutDocuments docModel in currentstake.OutDocuments)
{
if (!stApp.StakeholderOutDocuments.Any(t => t.OutDocumentId == docModel.OutDocumentId))
{
var result = await _service.DownloadBlob(allOutDocuments.FirstOrDefault(u => u.Id == docModel.OutDocumentId).Filename);
Attachments.Add(result.BlobFileName, result.BlobStream);
StakeholderOutDocument stakeholderOutDocument = new StakeholderOutDocument();
stakeholderOutDocument.OutDocumentId = docModel.OutDocumentId;
stakeholderOutDocument.Comment = docModel.Comment;
stakeholderOutDocument.StakeholderApplicationId = stApp.Id;
stApp.StakeholderOutDocuments.Add(stakeholderOutDocument);
hasDocuments = true;
}
}
if (!hasDocuments) continue;
db.Entry(stApp).State = EntityState.Modified;
}
}
db.SaveChangesAsync();
The StakeholderApplication
contains list of StakeholderOutDocuments
.
I am trying to add StakeholderOutDocument
objects into the StakeholderApplication
object.
Model:
public partial class StakeholderOutDocument
{
public int Id { get; set; }
public int StakeholderApplicationId { get; set; }
public int OutDocumentId { get; set; }
public string Comment { get; set; }
public virtual OutDocument OutDocument { get; set; }
public virtual StakeholderApplication StakeholderApplication { get; set; }
}
StakeholderApplication Model:
public partial class StakeholderApplication
{
public int Id { get; set; }
public virtual ICollection<StakeholderOutDocument> StakeholderOutDocuments { get; set; }
}
Once all the individual StakeholderOutDocument
s are added into the StakeholderApplication
, I add the StakeholderApplication
object as modified and call db.SaveChanges
. During when I get the error.
I am not sure why the error occurs. What am I doing wrong?
Upvotes: 0
Views: 3872
Reputation: 317
I have resolved this.. After a fair amount of search. I was able to find that the object being added was moved to List. and an save operation was done on it .. Removed the ToList and it worked.
I am not expert. But I think the ToList has detached the Data from the dbContext. Otherwords move the data into memory and thus the suring save EF treats it as a new entry and tries to update the old one thus causing the foreign keys to become null
http://www.codeproject.com/Articles/576393/Solutionplusto-aplus-Theplusoperationplusfailed
Thanks
Upvotes: 1