我已经构建了一个简单的文件管理器,它从两个表中提取数据,文件夹和文件。当您上传文件时,它会被写入服务器上的磁盘。当我需要删除一个文件夹及其所有子文件夹、子文件夹文件和父文件夹文件时,我选择该文件夹,然后单击删除,这将执行下面的控制器操作。
更新 如果我更改对 .ToList() 的任何后续调用,它会起作用。
[HttpPost]
public JsonResult DeleteFolder(int folderID)
{
var repo = new FileManagerRepository();
var folder = repo.GetFolder(folderID);
var subfolders = repo.GetSubFolders(folderID).ToList();
var files = repo.GetFiles(folderID).ToList();
// delete sub-folders
if (subfolders.Count() != 0)
{
foreach (var subfolder in subfolders)
{
// delete sub-folder files
var subfiles = repo.GetFiles(subfolder.folder_id).ToList();
if (subfiles.Count() != 0)
{
foreach (var file in subfiles)
{
repo.DeleteFile(file.file_id);
System.IO.File.Delete(Server.MapPath("/content/upload/" + file.file_name));
}
}
repo.DeleteFolder(subfolder.folder_id);
}
}
// delete files
if (files.Count() != 0)
{
foreach (var file in files)
{
repo.DeleteFile(file.file_id);
System.IO.File.Delete(Server.MapPath("/content/upload" + file.file_name));
}
}
// delete the folder
if (folder != null)
{
repo.DeleteFolder(folder.folder_id);
}
repo.Save();
return new JsonResult();
}
现在这里是 FileManagerRepository
public class FileManagerRepository
{
private readonly iau_idahoEntities db = new iau_idahoEntities();
public IQueryable<folders> GetParentFolders()
{
return db.folders.Where(f => f.parant_folder_id == 0);
}
public IQueryable<folders> GetSubFolders()
{
return db.folders.Where(f => f.parant_folder_id != 0);
}
public IQueryable<folders> GetSubFolders(int folder_id)
{
return db.folders.Where(f => f.parant_folder_id != 0 && f.parant_folder_id == folder_id);
}
public IQueryable<Files> GetFiles(int id)
{
return db.Files.Where(f => f.folder_id == id);
}
public Files GetFile(int id)
{
return db.Files.SingleOrDefault(f => f.file_id == id);
}
public void UpdateDownloadCount(int id)
{
var file = GetFile(id);
file.downloaded += 1;
}
public void AddFolder(int parentFolderID, string folderName)
{
var folder = new folders { create_date = DateTime.Now, parant_folder_id = parentFolderID, folder_name = folderName };
db.folders.AddObject(folder);
}
public void DeleteFolder(int folder_id)
{
var folder = db.folders.SingleOrDefault(f => f.folder_id == folder_id);
db.folders.DeleteObject(folder);
}
public void Save()
{
db.SaveChanges();
}
public void AddFile(int folder_id,string filename, string description)
{
var file = new Files {
folder_id = folder_id,
file_name = filename,
file_extension = filename.Substring(filename.Length - 3, 3),
description = description,
downloaded = 0,
physical_name = filename.Substring(0, filename.Length - 4),
upload_date = DateTime.Now
};
db.Files.AddObject(file);
}
internal void DeleteFile(int fileID)
{
var fileToDelete = db.Files.SingleOrDefault(file => file.file_id == fileID);
db.Files.DeleteObject(fileToDelete);
}
internal folders GetFolder(int folderID)
{
return db.folders.SingleOrDefault(folder => folder.folder_id == folderID);
}
}
问题是我不断收到以下 EF 异常: 已经有一个打开的 DataReader 与此命令关联,必须先关闭
我尝试将 MultipleActiveResultSets = true, 添加到 web.config 中的连接字符串,但仍然出现异常。我知道为什么会抛出这个异常,但我不知道如何做我需要做的事情并防止它抛出异常。
请帮忙!!!