2012-05-04 78 views
3

我有一系列代表文件夹和文件的对象。文件夹当然可以有一个文件集合,但他们也可以有子文件夹。文件夹有一个引用回到父文件夹。这可能是麻烦开始的地方。另外一个文件夹可以有一个与之相关的图标。EF代码第一个循环参考

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。

任何想法?

+0

FileInformations以任何方式涉及到此?我没有得到你展示的代码的循环参考。 –

+0

除此之外,你还在做任何流畅的配置吗? – NSGaga

+0

您是否找到答案?我正面临类似的情况。 – Shimmy

回答

0

您好改变Id而不是FolderId,IconId这是用[键]修改。因为您不使用映射流利的代码,并且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; } 
}