0
votes

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 StakeholderOutDocuments 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?

1
What foreign keys do your tables have? The exception is pointing you to a foreign key you're assigning a null value - n1ff
Hi Niff, Thanks.. Yes I checked it ... I have a model of the keys ... I also checked via debug that the key are assigned correct - Pradeep K
why do you need this db.Entry(stApp).State = EntityState.Modified; ? and what is the relationship between OutDocuments and OutDocument? I suspect this is the line that is causing problems: stakeholderOutDocument.OutDocumentId = docModel.OutDocumentId;. Try to db.StakeholderOutDocuments.Add(stakeholderOutDocument); and db.SaveChanges() after each iteration, and see specifically what document is causing the problem. - Cristi Pufu
OutDocuments is a custom model that has the id send from the frontend (by doing a get operation).I tried adding the db.savechanges but the error occurs the very first save call. - Pradeep K
is it possible to catch this error to find out where it occurs ? - Pradeep K

1 Answers

1
votes

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