I'm developing a Mega (file storage) like website in C# asp.net mvc as a personal project and have run into a bit of a snag. I have two classes, files and folders:
public class File
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string Checksum { get; set; }
public int Size { get; set; }
public virtual Folder ParentFolder { get; set; }
public virtual User Owner { get; set; }
}
public class Folder
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Folder ParentFolder { get; set; }
public virtual User Owner { get; set; }
}
Some elements aren't implemented yet so don't be surprised if some bits seem to be missing ;) I'm having a bit of a problem regarding the deletion of a folder:
public IHttpActionResult DeleteFolder(int id)
{
Folder folder = db.Folders.Find(id);
if (folder == null)
{
return NotFound();
}
FilesController filesController = new FilesController();
var Folders = db.Folders.Where(a => a.ParentFolder.Id == id);
var Files = db.Files.Where(b => b.ParentFolder.Id == id);
foreach (File f in Files)
{
filesController.DeleteFile(f.Id);
}
foreach (Folder f in Folders)
{
DeleteFolder(f.Id);
}
db.Folders.Remove(folder);
db.SaveChanges();
return Ok(folder);
}
This method is called from my FoldersController, it finds a list of files that belong to a given folder (id) and does the same for child folders. It then calls the DeleteFile method from the FilesController and removes all of them and recursively calls the DeleteFolder method again in order to delete the rest of the folder tree (the child part at least). The problem is that when I actually call the deleteFolder method, I get an "System.Data.Entity.Infrastructure.DbUpdateException" and can't seem to figure out why... Would anyone happen to have any ideas ? (could it be something like a concurrency control issue ?).