2012-05-04 65 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; } 
}