2017-03-21 56 views
1

我面臨的一個問題與描述不處理:C#類型「System.ArgumentException」的異常出現在EntityFramework.dll但在用戶代碼

類型「System.ArgumentException」的發生異常在EntityFramework.dll中,但沒有在用戶代碼中處理。

附加信息:包含路徑表達式必須引用在該類型上定義的導航屬性。對於參考導航屬性使用虛線路徑,對集合導航屬性使用Select運算符。

的問題顯示出來,當我把我的Getall方法

var gruppi = GroupHelper.GetAll().OrderBy(a => a.Order);

GETALL方法:

public new static ICollection<Group> GetAll() 
    { 
     using (var provider = new GroupProvider()) 
     { 
      provider.QAll().Include 
      (a => a.RoleGroup.Select(c => c.Role)). 
      Include(a=>a.GroupLanguage.Select(b=>b.Language). 
      Where(c=>c.LanguageName=="ENG")).ToList();    
     } 
    } 

GroupLanguageRoleGroup的屬性如下:

public class GroupLanguage 
{ 
    [Key, Column(Order = 1)] 
    public Guid LanguageID { get; set; } 
    [Key, Column(Order = 2)] 
    public int GroupID { get; set; } 
    [ForeignKey("LanguageID")] 
    public Language Language { get; set; } 
    [ForeignKey("GroupID")] 
    public Group Group { get; set; } 
} 

public class RoleGroup 
{ 
    [Key, Column(Order = 1)] 
    public Guid RoleId { get; set; } 
    [Key, Column(Order = 2)] 
    public int GroupId { get; set; } 
    [ForeignKey("RoleId")] 
    public Role Role { get; set; } 
    [ForeignKey("GroupId")] 
    public Group Group { get; set; } 
} 

任何想法爲什麼會發生這種情況?

+0

添加try-catch塊可能會幫助您找出解決此問題的步驟。你試過什麼了? – confusedandamused

+0

@confusedandamamused我試圖拆分查詢,但沒有成功,我得到了同樣的例外 –

回答

1

您無法過濾Include,您需要引用導航屬性。取下Where調用內部:

public new static ICollection<Group> GetAll() 
{ 
    using (var provider = new GroupProvider()) 
    { 
     provider.QAll().Include(a => a.RoleGroup.Select(c => c.Role)) 
        .Include(a=>a.GroupLanguage.Select(b=>b.Language)).ToList();    
    } 
} 

如果您需要加載只LanguageName=="ENG"GroupLanguage,那麼你應該突出你的查詢,但你會失去你的實體類型,因爲LINQ到實體唯一支持項目查詢到匿名對象或DTO,例如:

public new static ICollection<GroupDTO> GetAll() 
{ 
    using (var provider = new GroupProvider()) 
    { 
     provider.QAll().Include(a => a.RoleGroup.Select(c => c.Role)) 
        .Include(a=>a.GroupLanguage.Select(b=>b.Language)) 
        .Select(a=>new GroupDTO{GroupName=a.Name, 
              Language=a.GroupLanguage.Select(b=>b.Language) 
                    .Where(c=>c.LanguageName=="ENG")}) 
        .ToList();    
    } 
} 
+0

嗯好吧,我會嘗試它......但是GroupDTO假設是什麼?我在實體框架中有點新 –

+0

對不起,我應該解釋一下,這是一個自定義類,您可以使用您只需要的屬性來定義 – octavioccl

相關問題