Q:

Query DB loads one of my object's properties and it shouldn't

In my Edit Controller Action, I post the object to update.
 [HttpPost]
 public virtual ActionResult Edit(Case myCase){
      var currentDocuments = db.CaseDocuments.Where(p => p.idCase == myCase.idCase);

            foreach (CaseDocument docInDB in currentDocuments )
            {
                var deleteDoc = true;

                foreach (CaseDocument docNew in myCase.CaseDocuments )
                {
                    if (docNew.idDocument == docInDB.idDocument)
                        deleteDoc = false;
                }
                if (deleteDoc )
                    db.CaseDocuments.Remove(docInDB);
            }

            foreach (CaseDocument pc in myCase.CaseDocuments)
            {
                if (pc.idDocument == 0)
                    db.CaseDocuments.Add(pc);
                else
                    db.Entry(pc).State = EntityState.Modified;    
            }

           *** **db.Entry(myCase).State = EntityState.Modified;**  //THIS LINE
            db.SaveChanges();
 }

The Case model has a collection of Documents, and they are posted along with the Case Model.
As soon I enter the action, I can count the number of documents in the collection, and lets say there are 3.  
Then, in order to see if I need to delete documents from database (as the user deleted one from UI), I need to get the Documents for that case from database in this way:
var currentDocuments = db.CaseDocuments.Where(p => p.idCase == myCase.idCase);

And here starts the weird thing: as soon I executa that statement, the myCase.Documents is loaded with what it is in database (lets say there are 4)!!  So, I'm not able to compare the 2 collections (to detect if a document was deleted and remove it from db).
What I need is during the Edit Action of my Case model, I need to create/update/modify its documents. Do I need to see this from other angle?  What I'm doing is wrong?
EDIT:
After the comments, I realized that the line where I marked myCase as Modified, was at the begining, and I suppose that this was the reason for that behaviour.
Now, moving that line to just before the db.SaveChanges(), fixed that problem, but at the db.Entry(myCase).State = EntityState.Modified;  says "There is already an object with the same key in ObjectStateManager. "
What am I doing wrong here?  This code looks bad!

A:

Try it this way:
 [HttpPost]
 public virtual ActionResult Edit(Case myCase){
     var currentDocumentIds = db.CaseDocuments
                                .Where(p => p.idCase == myCase.idCase)
                                .Select(p => p.idDocument);

     foreach (int idInDb in currentDocumentsIds.Where(i => !myCase.CaseDocuments
                                                                  .Any(ci => ci.idDocumnet == i))
     {
         var docToDelete = new CaseDocument { idDocument = idInDb };
         db.CaseDocuments.Remove(docToDelete);
     }

     foreach (CaseDocument pc in myCase.CaseDocuments)
     {
         if (pc.idDocument == 0)
            db.CaseDocuments.Add(pc);
         else
            db.Entry(pc).State = EntityState.Modified;    
     }

     db.Entry(myCase).State = EntityState.Modified;
     db.SaveChanges();
}

Edit: The difference between this code and your code is the way how it works with existing documents. It doesn't load them - it loads just their ids. This way you will save some data transfer from database but it should also help you avoiding that exception. When you load the document from the database you have it already attached in the context but if you try to call this:
db.Entry(pc).State = EntityState.Modified; 

you will try to attach another instance of the document with the same key to the context. That is not allowed - context can have attached only single instance with unique key. 

