简体   繁体   English

递归树方法上的System.Data.Entity.Infrastructure.DbUpdateException

[英]System.Data.Entity.Infrastructure.DbUpdateException on recursive Tree method

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. 我正在将C#asp.net mvc中的Mega(文件存储)网站开发为个人项目,并且遇到了一些麻烦。 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. 从我的FoldersController中调用此方法,它会找到属于给定文件夹(id)的文件列表,并对子文件夹执行相同的操作。 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). 然后,它从FilesController调用DeleteFile方法并将其全部删除,然后再次递归调用DeleteFolder方法以删除其余的文件夹树(至少是子部分)。 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 ? 问题是,当我实际调用deleteFolder方法时,我得到一个“ System.Data.Entity.Infrastructure.DbUpdateException”,似乎无法弄清楚为什么...有人会碰巧有什么想法吗? (could it be something like a concurrency control issue ?). (这可能是并发控制问题吗?)。

Take a look at this post here . 这里看看这篇文章。

Just by removing the file from the folder you are not labeling the file for deletion. 仅从文件夹中删除文件,就不会将文件标记为删除。 You have to actually call DeleteObject on the file. 您必须实际在文件上调用DeleteObject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException System.Data.Entity.Infrastructure.DbUpdateException问题 - System.Data.Entity.Infrastructure.DbUpdateException Issue savechanges方法中的“ System.Data.Entity.Infrastructure.DbUpdateException” - 'System.Data.Entity.Infrastructure.DbUpdateException' within savechanges method 发生System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException occurred MVC EF-System.Data.Entity.Infrastructure.DbUpdateException - MVC EF - System.Data.Entity.Infrastructure.DbUpdateException 如何解决 System.Data.Entity.Infrastructure.DbUpdateException - How to solve System.Data.Entity.Infrastructure.DbUpdateException 将数据添加到数据库(错误:System.Data.Entity.Infrastructure.DbUpdateException) - Adding Data to Database (Error: System.Data.Entity.Infrastructure.DbUpdateException) EntityFramework.dll 中发生“System.Data.Entity.Infrastructure.DbUpdateException” - 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll 如何避免System.Data.Entity.Infrastructure.DbUpdateException - How to avoid System.Data.Entity.Infrastructure.DbUpdateException 发生类型为“ System.Data.Entity.Infrastructure.DbUpdateException”的异常 - An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM