我构建了一个简单的文件管理器,它从两个表,文件夹和文件中提取数据。 上传文件时,它会写入服务器上的磁盘。当我需要删除文件夹及其所有子文件夹,子文件夹文件和父文件夹文件时,我选择文件夹,然后单击删除,执行下面的控制器操作。
更新 如果我将任何后续调用更改为.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中的连接字符串中,但我仍然得到异常。我知道为什么会抛出这个异常,但我不知道如何做我需要做的事情并防止它抛出异常。
请帮助!!!
答案 0 :(得分:1)
将var subfolders = repo.GetSubFolders(folderID);
行更改为var subfolders = repo.GetSubFolders(folderID).ToList();
答案 1 :(得分:0)
如果我将对存储库的所有后续调用更改为ToList(),则所有调用都应该如此。
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();