2017-05-30 66 views
0

多個表我有3個表像下圖包括帶的EntityFramework

Tables

而且我codefirst在C#(自動生成)

我試圖讓模型中的實體框架IEnumerable包含每個元素的包含附件列表的實體,我希望這些附件元素包含在每個附件的AttachmentType中。

更好的示範,我需要:

IEnumerable<Registry> registries = GetTableData(); 
string oneofthedesiredname = registries 
.First() 
.Attachments 
.First() 
.AttachmentType.Name; //I want that, this definition works but its null 

與GetTableData()方法是像

public IEnumerable<Registry> GetTableData() 
    { 
     IQueryable<Registry> _registries = _entities.Registry; 
     IEnumerable<Registry> data; 
     data = _registries 
     .Where(p=>p.IsDeleted==false) 
     .Include(p=>p.Attachments.Where(x=>x.IsDeleted==false)) 
     .AsEnumerable();//this query should have change because I cant get Attachmenttypes from this    
     return data; 
    } 

謝謝大家,您的幫助表示讚賞。

+0

也許我是盲目的,但我不能找到一種方法,從Rgistries去附件只有附件知道登記處。您可以請您發佈您的EF代碼模型? – Taysumi

回答

0

您還必須在查詢中添加額外的Include for AttachmentType。

public IEnumerable<Registry> GetTableData() 
{ 
    IQueryable<Registry> _registries = _entities.Registry; 

    IEnumerable<Registry> data = _registries 
     .Include(x => x.Attachments) 
     .Include("Attachments.AttachmentType") 
     // here do necessary filtering with Where() 
     // ... 

    return data; 
} 
0

使用本funcation

public IEnumerable<Registry> GetTableData() 
    { 
     IQueryable<Registry> _registries = _entities.Registry,where(m=>m.IsDeleted==false); 
     IEnumerable<Registry> data; 
     data = _registries  .Include(p=>p.Attachments.Where(x=>x.IsDeleted==false)). 
include(p=>p.AttachmentType).AsEnumerable(); 
     return data; 
    }