3

我有一系列代表文件夹和文件的对象。文件夹当然可以有一组文件,但它们也可以有子文件夹。文件夹具有对父文件夹的引用。这可能是麻烦开始的地方。一个文件夹也可以有一个与之关联的图标。

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; }
}

但是,当我运行应用程序并尝试获取图标列表时,我收到以下错误消息:

*引用关系将导致不允许的循环引用。[ 约束名称 = FK_Folder_Icon_IconId ]*

我不是 100% 在这里循环引用。文件夹只引用一次图标,图标根本不引用文件夹。

一个问题,这可能是相关的,是我不确定如何正确地将 ParentFolderId 映射回父文件夹的 FolderId。

有任何想法吗?

4

1 回答 1

0

嗨更改 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; }
}
于 2012-06-05T07:18:58.013 回答