EF Code First Cyclical Reference

I have a series of objects that represent folders and files. Folders can have a collection of files, but they can also have subfolders. The folder has a link to the parent folder. Most likely, the problem begins here. A folder may also have an icon associated with it.

public class Folder
{
    [Key]
    public int FolderId { get; set; }
    public string FolderName { get; set; }
    public int ParentFolderId { get; set; }
    public virtual Folder ParentFolder { get; set; }
    public int IconId { get; set; }
    public virtual Icon Icon { get; set; }

    public virtual ICollection<FileInformation> FileInformations { get; set; }
    public virtual ICollection<Folder> Folders { get; set; }
}

public class Icon
{
    [Key]
    public int IconId { get; set; }
    public string IconUrl { get; set; }
    public string Description { get; set; }
}

When I launch the application and try to get a list of icons, I get this error message:

* A related relation will result in a circular reference that is not allowed. [Constraint name = FK_Folder_Icon_IconId] *

I'm not 100% where the circular link is here. The folder only refers to the icons once, and the icon does not refer to the folder at all.

One of the problems, and this may be related, is that I'm not sure how to properly convert the ParentFolderId to the FolderId folder of the parent folder.

?

+3
1

, Id FolderId, IconId, [key]. , EF .

.

public class Folder
{
    [Key]
    public int Id { get; set; }

    public string FolderName { get; set; }
    public virtual int ParentId { get; set; } /*ParentFolderId*/
    public virtual Folder Parent { get; set; } /*ParentFolder*/
    public virtual int IconId { get; set; }
    public virtual Icon Icon { get; set; }

    public virtual ICollection<Folder> Children { get; set; } /*not Folders*/

   //it is out of subject 
   //public virtual ICollection<FileInformation> FileInformations { get; // set; }
}

public class Icon
{
    [Key]
    public int Id { get; set; }

    public string IconUrl { get; set; }
    public string Description { get; set; }
}
0

All Articles