Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8393073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:41:27+00:00 2026-06-09T19:41:27+00:00

This is my code: public void DeleteFolder(Entities.DocumentFolder folder) { DeleteFilesFromServer(folder.Id); _dbContext.Entry(folder).State = EntityState.Deleted; _dbContext.SaveChanges();

  • 0

This is my 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 => x.FolderId == id).ToList();
       foreach (var filePath in allDocuments.Select(document => 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<Document>();
      }
      public int Id { get; set; }
      public string FolderName { get; set; }
      public int ParentFolderId { get; set; }
      public List<Document> 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; }
  }

By executing the delete operation I got the following exception:

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.

If I remove the DeleteFilesFromServer(int id) method the deletion working. Can someone help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:41:28+00:00Added an answer on June 9, 2026 at 7:41 pm

    If you want to delete the DocumentFolder, you need to delete the Document objects related to the DocumentFolder because in your model the field Folder is not nullable. This happens only if the dbContext knows that the the Document objects exist, i.e. if you load the documents with the Select method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have this code: public void MyMethod(string Data, List<string> InputData) { //I
I have this code: @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { GLES20.glClearColor(0, 0,
Take a look at this code: public class Test { public static void main(String...
This is my code : public void AddToGrid(string value) { MessageBox.Show(value); //Message is showing
i have this code public interface Interfcae1 { void OP1(); } public interface Interfcae2
In facebook tutorials I see this sample code: @Override public void onActivityResult(int requestCode, int
I try to update database table and this is my code: public void updatefiletable(String
This code is invalid: interface Foo { public void foo(final String string); } public
Given this piece of code : public static void writeFile(File file,List buffer)throws IOException{ File
Try this piece of code - public class WhitespaceTest { public static void main(String[]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.