Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity FrameWork 4.1 Exception
    primarykey
    data
    text
    <p>This is my code:</p> <pre><code> public void DeleteFolder(Entities.DocumentFolder folder) { DeleteFilesFromServer(folder.Id); _dbContext.Entry(folder).State = EntityState.Deleted; _dbContext.SaveChanges(); } public void DeleteFilesFromServer(int id) { var allDocuments = _dbContext.Document.Where(x =&gt; x.FolderId == id).ToList(); foreach (var filePath in allDocuments.Select(document =&gt; HttpContext.Current.Server.MapPath("~/Documents/") + document.DocumentFileName).Where(System.IO.File.Exists)) { System.IO.File.Delete(filePath); } } public class DocumentFolder { public DocumentFolder() { Documents=new List&lt;Document&gt;(); } public int Id { get; set; } public string FolderName { get; set; } public int ParentFolderId { get; set; } public List&lt;Document&gt; Documents { get; set; } } public class Document { public int Id { get; set; } public string DocumentName { get; set; } public string DocumentFileName { get; set; } public int FolderId { get; set; } public virtual DocumentFolder Folder { get; set; } } </code></pre> <p>By executing the delete operation I got the following exception:</p> <blockquote> <p>System.InvalidOperationException: 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.</p> </blockquote> <p>If I remove the <code>DeleteFilesFromServer(int id)</code> method the deletion working. Can someone help me?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload